โŒ

Normal view

There are new articles available, click to refresh the page.
Today โ€” 27 April 2024Main stream

Python Hashicorp Vault library 'hvac' creates a new secret version but removes keys from previous version

I'm using the Python library 'hvac' to access Hashicorp Vault and to create/update an existing key.

In Hashicorp Vault, I have 2 key/value pairs listed for the latest version. For example,

k1=111

k2=222

After I run the Python code below, a new 'version' is created in the Vault UI with the new 'k1' key value, but I loose the 'k2' key and value.

import hvac

client = hvac.Client(url='http://localhost:8200', token='hvs.xxxxxxxxxx')
client.secrets.kv.v2.create_or_update_secret(path='foo',secret=dict(k1='test123'))

Does anyone know what I'm doing wrong? I'm trying to perform the equivalent of 'create new version' from the Vault UI but within python.

Thanks for any help!

Inverted hierarchy of nested dicts/lists

Considering the following dictionary:

{
  "a": {
    "b1": {
      "c": {
        "value": "c_value",
        "children": {
          "d": "d_value"
        }
      }
    },
    "b2": [
      "b2_1",
      {
        "b2_2_1": 2,
        "b2_2_2": {
          "b2_2_2_1": "b2_2_2_1_value",
          "b2_2_2_2": "b2_2_2_2_value"
        }
      },
      3
    ]
  }
}

how to reverse it such as the result is this:

{
  "d": {
    "children": {
      "c": {
        "b2": [
          3,
          {
            "b2_2_2": {
              "b2_2_2_2": "b2_2_2_2_value",
              "b2_2_2_1": "b2_2_2_1_value"
            },
            "b2_2_1": 2
          },
          "b2_1"
        ],
        "b1": {
          "a": "d_value"
        }
      }
    },
    "value": "c_value"
  }
}

I'm struggling with a recursive approach to this problem, without the need of creating a global variable to handle the results.

Smart elevator optimization

I have a problem of an elevator which decides the minimum amount of stops for a given floor pressed by the riders. Riders can be dropped of not necessarily at their desired floor, instead their closest floor, if the walking distance to their floor is not large (say 3 floors max). Also, we can assign k = maximum stops elevator is allowed.

How can I solve this dynamically, here is a sample output: D = [1, 3, 6] # Floors from riders k = 2 # Maximum stops

Output: 2, 6

I have defined that : minCost[i][j] as the minimum cost of serving all the riders using exactly j stops, with the last stop being on floor i. And conclude that

  • m is a 2d table
  • i is the last stop location
  • j is the number of stops

and have the recurrence: m[k][j] - cost of all above k + cost from k to i + cost of i to above

VueJS: Tracking the order of child components in a parent slot at run time

I have a custom component where the parent and an array of child components are tightly coupled and there is a "backdoor" communication between the parent and child components using the provide-inject feature provided by vue.

In the parent component, I would like to keep track of the child components in the order they are defined in the slot.

I know that on mounted, the child components are instantiated in the order they are defined. However, at runtime, a child component might be inserted in between first child component and second child component for example. The question is how do I find out the updated order of the child components in the parent slot?

I saw in the vue devtools, it can recognise the order of child and nested components in the Components tab and show it in a tree layout. How can I achieve something like that?

Is there a workaround for clang 16 not handling niebloid with template friend function?

I have some code that defines a niebloid as such:

#define FWD(...) static_cast<decltype(__VA_ARGS__)&&>(__VA_ARGS__)

namespace mylib {
    namespace detail {
        auto func(struct poison&) = delete;

        struct func_function {
            constexpr auto operator()(auto&& a) const -> decltype(func(FWD(a))) {
                return func(FWD(a));
            }
        };
    }

    inline namespace niebloid {
        inline constexpr auto func = detail::func_function{};
    }

    template<typename T>
    struct some_type {
        template<typename U>
        friend constexpr auto func(U&&) -> int {
            return 42;
        }
    };
}


int main() {
    return mylib::func(mylib::some_type<int>{});
}

Everything looks good, except for clang 16 specifically.

If we look at this conformance view, we can observe older version of clang down to clang 11 handles this code very well, and clang 17 and up also handles it quite well.

But clang 16 and only 16 seem to not be able to compile this code.

Making some_type non template or making the hidden friend non template too seems to not trigger the bug, but both options are not available for my codebase.


Now for the question. Is there a most minimally disruptive workaround I could use to make this code work with clang 16 or this compiler is inherently incompatible with niebloid like patterns?

Unable to create CosmosDB database via go sdk

From Mac OS X, using the emulator for Azure CosmosDB running on a Docker container locally, I am able to use the explorer web portal to create a database. However, I am unable to use the Azure Go SDK to create a database, but the errors don't present that there was an issue creating the db.

Besides this, there are multiple SDKs and a lot of the documentation has mistakes in it, is there a canonical source where I can see a functioning Golang example of utilizing the CosmosDB emulator?


cred, err := azcosmos.NewKeyCredential(cosmosDbKey)
    handle(err)
    client, err := azcosmos.NewClientWithKey(cosmosDbEndpoint, cred, nil)
    handle(err)

    databaseProperties := azcosmos.DatabaseProperties{ID: "databaseName"}
    databaseResponse, err := client.CreateDatabase(context.Background(), databaseProperties, nil)

How can I get better visibility into what is going on here, the client is able to create even if I pass in empty strings instead of the proper key and endpoint.

Tried to use the Go SDK to create a database, was expecting it to appear in the emulator portal. I would also expect NewClientWithKey() to fail when the credentials are invalid, this is not the case.

Whats the difference between isAssignableFrom and instanceof?

Taking the code below into consideration:

@RestControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
    @Resource
    private MessageSource messageSource;
    private HttpHeaders headers(){
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        return headers;
    }
    private ResponseError responseError(String message,HttpStatus statusCode){
        ResponseError responseError = new ResponseError();
        responseError.setStatus("error");
        responseError.setError(message);
        responseError.setStatusCode(statusCode.value());
        return responseError;
    }
    @ExceptionHandler(Exception.class)
    private ResponseEntity<Object> handleGeneral(Exception e, WebRequest request) {
        if (e.getClass().isAssignableFrom(UndeclaredThrowableException.class)) {
            UndeclaredThrowableException exception = (UndeclaredThrowableException) e;
            return handleBusinessException((BusinessException) exception.getUndeclaredThrowable(), request);
        } else {
            String message = messageSource.getMessage("error.server", new Object[]{e.getMessage()}, null);
            ResponseError error = responseError(message,HttpStatus.INTERNAL_SERVER_ERROR);
            return handleExceptionInternal(e, error, headers(), HttpStatus.INTERNAL_SERVER_ERROR, request);
        }
    }
    @ExceptionHandler({BusinessException.class})
    private ResponseEntity<Object> handleBusinessException(BusinessException e, WebRequest request) {
        ResponseError error = responseError(e.getMessage(),HttpStatus.CONFLICT);
        return handleExceptionInternal(e, error, headers(), HttpStatus.CONFLICT, request);
    }
}

What would be the biggest difference between replacing

if (e.getClass().isAssignableFrom(UndeclaredThrowableException.class)) {

with

if (e instanceof UndeclaredThrowableException) {

Line 19.

I found some explanations but none detailed enough.I wanted to understand the situations in which I should use isAssignableFrom or instanceof.

Python: indexes of all keys being changed in a dictionary within a class's object instead of just one key

(Both Classes are at the bottom of the post) Im trying to make a "world" in python looking from the top, the world is made up of a dictionary like

WorldDict = 
{
0: ["-","-","-"],
1: ["-","-","-"],
2: ["-","-","-"]
}

I'm trying to "spawn" a player into the world and im gettting stuck on the part where it "renders" the player(changing a - to a p in the coordinates on the dictionary).

This is the starting code:

import WorldClass
import CreatureClass

MyWorld = WorldClass.World(5, 5, "  -")
MyWorld.CreateWorld()

Player = CreatureClass.Creature("  P", "player", 100, 1, 10, 1, "1")

Doing MyWorld.PrintWorld() will just print the dictionary in a nice format. Current output:

  -  -  -  -  -
  -  -  -  -  -
  -  -  -  -  -
  -  -  -  -  -
  -  -  -  -  -

Heres the problem, When I do MyWorld.SpawnCreature(" P", Player) or even just directly changing the dict:

coordinates = (1,3)
MyWorld.WorldDict[coordinates[1]][coordinates[0]] = "  P"

it ends up making it look like this:

  -  P  -  -  -
  -  P  -  -  -
  -  P  -  -  -
  -  P  -  -  -
  -  P  -  -  -

Can someone tell me why its affecting the all of the indexes at 1 instead of just the one at 3? What I want:

  -  -  -  -  -
  -  -  -  -  -
  -  -  -  -  -
  -  -  -  -  -
  -  P  -  -  -

Classes:

World:

import CreatureClass
class World:
    def __init__(self, width, height, defaultchar):
        self.width = width
        self.height = height
        self.defaultchar = defaultchar
        self.WorldDict = {}

    def CreateWorld(self):

        defaultchunk = []

        for number in range(0, self.width):
            defaultchunk.append(self.defaultchar)

        for number in range(0, self.height):
            self.WorldDict[number] = defaultchunk

    def PrintWorld(self):
        WorldDictKeys = list(self.WorldDict.keys())
        for key in WorldDictKeys:
            print("".join(self.WorldDict[key]))

    def GetCoords(self, coords):
        if self.CoordsValid(coords):
            item = self.WorldDict[coords[1]][coords[0]]
            return item

    def LocationIsEmpty(self, coords):
        if self.CoordsValid(coords):
            if self.GetCoords(coords) == self.defaultchar:
                return True
            else:
                return False

    def DrawOnWorld(self, coords, symbol):
        if self.CoordsValid(coords):
            self.WorldDict[coords[1]][coords[0]] = symbol
            print(f"drew on points ({coords[0]},{coords[1]})")


    def ClearCoords(self, coords):
        if self.CoordsValid(coords):
            self.DrawOnWorld(coords, self.defaultchar)


    def SpawnCreature(self, coords, aCreature):
        if self.CoordsValid(coords):
            if aCreature.creaturehasbeenspawned:
                print("creature has already been spawned")
            else:
                aCreature.creaturehasbeenspawned = True
                aCreature.coords = coords
                self.DrawOnWorld(aCreature.coords, aCreature.symbol)


    def CoordsValid(self,coords):
        if coords[0] <= len(self.WorldDict[0]) - 1 and coords[1] in list(self.WorldDict.keys()) and coords[0] >= 0 and coords[1] >= 0:
            return True
        else:
            return False
    def MoveCreature(self, aCreature, coords):
        if self.CoordsValid(coords):
            beforecoords = aCreature.coords

            aCreature.coords = coords
            self.ClearCoords(beforecoords)
            self.DrawOnWorld(aCreature.coords, aCreature.symbol)

    def TryMoveCreature(self, aCreature, coords):
        if self.CoordsValid(coords):
            if self.LocationIsEmpty(coords):
                self.MoveCreature(aCreature, coords)

Creature:

class Creature:
    def __init__(self, symbol, type, defaulthealth, defaultmovespeed, defaultdamage, skill, team):
        self.symbol = symbol
        self.type = type
        self.defathealth = defaulthealth
        self.defatmovespeed = defaultmovespeed
        self.defatdamage = defaultdamage
        self.skill = skill
        self.team = team
        self.health = defaulthealth
        self.movespeed = defaultmovespeed
        self.damage = defaultdamage
        self.creaturehasbeenspawned = False
        self.coords = None

I already described what happened, what I wanted to happen, and what I tried within the description of the post.

How to delete one commit completely?

I have some orphaned commits accidentally committed to GitHub.

I use git reflog expire --expire-unreachable=now --all to remove them from git rev-list --all. (I tried use git gc --prune=now --aggressive but maybe the repo is too big, it crashes and make all windows closed. I use Arch Linux and Kde6. Maybe My 16GB memory is a bit small.)

But git show foo_orphan_commit still shows the commit contents.

Then is there one way to remove that commit completely? I expects the commit won't be showed in Github. For example, if the commit to delete has message "#1", then in the Github #1 issue it won't show something like "Someone added a commit that referenced this issue ... foo_orphan_commit".

How to Set a Query-specific Timeout in gremlin_python for AWS Neptune?

I am using the gremlin_python library to execute a query on an AWS Neptune database. I have specified a timeout using the evaluationTimeout parameter in the query to handle long-running requests. Despite setting this parameter to MAX_QUERY_TIMEOUT, the query times out before reaching this limit. Here is the specific query causing the issue:

count_companies_with_representatives = (
    self.reader_g.V()
    .with_("evaluationTimeout", MAX_QUERY_TIMEOUT)
    .hasLabel("company")
    .where(__.bothE("director").count().is_(P.gt(0)))
    .count()
    .next()
)

I need each query to respect the set timeout to manage performance and avoid premature terminations.

I tried setting the evaluationTimeout directly in the query as shown above, expecting that this would prevent any timeouts shorter than MAX_QUERY_TIMEOUT. However, the query still times out earlier than anticipated, which suggests that either the timeout setting is not being respected, or I have misunderstood its usage. I was expecting that the timeout would ensure the query could run up to the maximum time allowed before stopping, thus giving it enough time to complete under normal conditions. I'm looking for guidance on how to correctly apply timeouts to individual queries in this setup or understanding if there's a better approach to managing query execution times.

How to read from the BigQuery rows instead of TableRows

I'm reading data from the BigQuery as TableRows, then convert them to Rows. This step takes a lot of time. Is it possible to read Rows from the beginning? Or any other ideas of how to make it faster?

p.apply("BQ " + tableName, BigQueryIO.readTableRows().from(table))
                .apply(ParDo.of(new TableRowToRowConverter(BQSchema)))
                .setRowSchema(BQSchema);
public class TableRowToRowConverter extends DoFn<TableRow, Row>  implements Serializable {

    private static final long serialVersionUID = 1L;
    
    private final Schema schema;

    public TableRowToRowConverter(Schema schema) {
        this.schema = schema;
    }

    @ProcessElement
    public void processElement(ProcessContext c) {
        TableRow tableRow = c.element();
        Map<String, Object> fieldValues = new HashMap<>();
        assert tableRow != null;
        schema.getFieldNames().forEach(e -> {
            try {
                fieldValues.put(e, tableRow.get(e)!= null ? tableRow.get(e) : "");
            } catch (Exception e1) {
            }
        });
        Row modifiedRow = Row.withSchema(schema).withFieldValues(fieldValues).build();

        assert tableRow != null;
        c.output(modifiedRow);
    }
}

How does one use vllm with pytorch 2.2.2 and python 3.11?

Title: How does one use vllm with pytorch 2.2.2 and python 3.11?

I'm trying to use the vllm library with pytorch 2.2.2 and python 3.11. Based on the GitHub issues, it seems vllm 0.4.1 supports python 3.11.

However, I'm running into issues with incompatible pytorch versions when installing vllm. The github issue mentions needing to build from source to use pytorch 2.2, but the pip installed version still uses an older pytorch.

I tried creating a fresh conda environment with python 3.11 and installing vllm:

$ conda create -n vllm_test python=3.11
$ conda activate vllm_test
(vllm_test) $ pip install vllm
...
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
vllm 0.4.1 requires torch==2.1.2, but you have torch 2.2.2 which is incompatible.

I also tried installing pytorch 2.2.2 first and then vllm:

(vllm_test) $ pip install torch==2.2.2
(vllm_test) $ pip install vllm
...
Building wheels for collected packages: vllm
  Building wheel for vllm (pyproject.toml) ... error
  error: subprocess-exited-with-error
  
  ร— Building wheel for vllm (pyproject.toml) did not run successfully.
  โ”‚ exit code: 1

Can someone clarify what versions of vllm, pytorch and python work together currently? Is there a recommended clean setup to use vllm with the latest pytorch 2.2.2 and python 3.11?

I've tried creating fresh conda environments, but still run into version conflicts. Any guidance on the right installation steps would be much appreciated. Thanks!

ref: https://github.com/vllm-project/vllm/issues/2747

Most efficent way to find all rows where row value is X2 greater than the previous two rows combine

In this scenario, I'll have a table that has column that is Fruit, this column will have about 1000 + distinct entries in it an a entry on a per day basis. What I'm trying to do is for each distinct type of Fruit, I want to find where the day has sold 2x the amount of fruit from the previous two days combined.

So for example, if on day 1&2, there was 1 apple sold each day I would have a total of 2 sold, I want to know whenever the following day has sold 2x the previous, so if day 3 has 4 apples sold then this is a positive result, however if 3 were sold, then this would be a negative result. I've put together a very clunky and slow cursor that I'm not confident in the results. One of the things to take into consideration is the data always need to be ordered by date desc, sometime the data isn't populated everyday an the ID will be out of order.

Here is a test query that has the final results I'm looking for.

declare @testTable table(Id int not null identity(1,1),Fruit nvarchar(10),Sold int, DateSold date)

DECLARE @StartDate datetime = '2024-01-22';
DECLARE @EndDate   datetime = Dateadd(day,100,@StartDate);

WITH theDates AS
     (SELECT @StartDate as theDate
      UNION ALL
      SELECT DATEADD(day, 1, theDate)
        FROM theDates
       WHERE DATEADD(day, 1, theDate) <= @EndDate
     )
     insert into @testTable(Fruit,Sold,DateSold)
SELECT 'Apple',90+ROW_NUMBER() OVER(ORDER BY theDate),theDate as theValue
  FROM theDates
  union 
  SELECT 'Orange',90+ROW_NUMBER() OVER(ORDER BY theDate),theDate as theValue
  FROM theDates
    union 
  SELECT 'Pears',90+ROW_NUMBER() OVER(ORDER BY theDate),theDate as theValue
  FROM theDates
    union 
  SELECT 'Plums',90+ROW_NUMBER() OVER(ORDER BY theDate),theDate as theValue
  FROM theDates
OPTION (MAXRECURSION 0)

Fiddle with sample data:

https://dbfiddle.uk/coq20xzk

declare @appleRow int = (SELECT TOP 1 Id FROM @testTable where fruit = 'Apple' ORDER BY NEWID());
declare @ornageRow int = (SELECT TOP 1 Id FROM @testTable where fruit = 'Orange' ORDER BY NEWID());
declare @pearRow int = (SELECT TOP 1 Id FROM @testTable where fruit = 'Pears' ORDER BY NEWID());
declare @plumRow int = (SELECT TOP 1 Id FROM @testTable where fruit = 'Plums' ORDER BY NEWID());

update @testTable
set Sold = Sold + 250
where Id in (@appleRow,@ornageRow,@pearRow,@plumRow);

select * from @testTable;

--these are the ids am looking for
select * from @testTable
where Id in (@appleRow,@ornageRow,@pearRow,@plumRow);

How to fix QPainterPath drawing tool not creating new paths each time I draw?

I have a function that when you click a point it draws a path between that clicked point and start point. I do have a checkable button (self.button) that activates and disables the tool, so I am trying to intergrate that. However, all of this has a major issue: I cannot draw multiple paths without it connecting from the last point of the original one, meaning I get this one infinetly drawable line. No matter me disabling or re-enabling the tool, it still starts from the original first drawn path.

def __init__(self, scene, button1, button2)
    self.points = []
    self.canvas = scene
    self.button = button1
    # ...

def mousePressEvent(self, event):
    if self.button.isChecked():
        if event.button() == Qt.MouseButton.RightButton:
            self.points.append(self.mapToScene(event.pos()))

            if len(self.points) > 1:
                path = QPainterPath()

                path.moveTo(self.points[0])

                for point in self.points[1:]:
                    path.lineTo(point)  # Add line segment to existing path

                # Create and configure CustomPathItem only once (outside loop)
                if not hasattr(self, "path_item"):
                    self.path_item = CustomPathItem(path)
                    self.path_item.setPen(self.pen)
                    if self.button3.isChecked():
                        self.path_item.setBrush(QBrush(QColor(self.stroke_fill_color)))
                    self.path_item.setZValue(0)
                    self.path_item.setFlag(QGraphicsItem.GraphicsItemFlag.ItemIsSelectable)
                    self.path_item.setFlag(QGraphicsItem.GraphicsItemFlag.ItemIsMovable)
                    self.canvas.addItem(self.path_item)

                # Update path of existing item within the loop
                self.path_item.setPath(path)

            super().mousePressEvent(event)

I don't know if I explained my issue, so if clarification is needed, please ask. Any help is appreciated.

Study warns users about health information on TikTok

The Chinese social network is packed with information of all kinds and in all fields, which can get up to tens of millions of views. This may be of little importance when it comes to showcasing the latest blush, but it can be crucial when the posts in question concern usersโ€™ health. Indeed, a new study reveals that much of this information is not based on factual evidence. Read full story

While some health information circulating on social networks is reliable, some of it can be considered โ€˜harmfulโ€™, researchers say. โ€” AFP Relaxnews

Skywatchers out in force

The recent total solar eclipse on April 4 was a highly anticipated event visible in parts of North America, including Mexico, the United States and Canada. The celestial event brought together astronomers, enthusiasts and just plain curious observers to witness the wonders of the universe. Read full story

1. A family visiting from Sarasota, Tampa, watching the solar eclipse at the Magic Kingdom at Walt Disney World, in Lake Buena Vista, Florida, United States. โ€” AP

QuickCheck: Has the e-Filing submission deadline been extended?

MARCH might have already come and gone, there are a few hold-outs who have yet to submit their tax returns for the the financial year of 2023. There are many reasons for someone to submit their returns late and those that do are usually hit with a stiff penalty. Read full story

โŒ
โŒ