admin


Recent Questions
Recent Answers
  • Query for model instances that are indirectly related

    8 hours ago

    You can .filter(…) [Django-doc] with:

    Review.objects.filter(cart__job__complete=True)

    One can use double underscores (__) to look "through" relations.

    This will thus retrieve all Reviews for which a related Cart exists for which a related Job exists that has complete=True.

  • Nested lists - changing the structure of a nested list (nesting reduction)

    11 hours ago

    Use from_iterables from itertools

    import itertools
    
    flat_list = list(itertools.chain.from_iterable(training_data))
    

  • How to create a vertical gradient bar with animation in it?

    15 hours ago

    div {
      height: 400px;
      width: 20px;
      background: linear-gradient(0deg, #ffee00, #ff0000, #ff00ff);
      background-size: 200% 200%;
    
      -webkit-animation: AnimationName 5s ease infinite;
      -moz-animation: AnimationName 5s ease infinite;
      animation: AnimationName 5s ease infinite;
    }
    
    @keyframes AnimationName {
        0%{background-position:50% 0%}
        50%{background-position:50% 100%}
        100%{background-position:50% 00%}
    }
    <div></div>

  • How to show all fields values of a model from a form in Django?

    1 day ago

    You are swapping the instance=s. You provided the usario_form the user object, and the user_form the usario object. You need to do the opposite:

    def modificar_cadastro(request):
        if request.method == 'POST':
            # …
        else:
            #                           not request.user ↓
            usuario_form = UsuarioForm(instance=request.user.usuario)
            #              not request.user.usario ↓
            user_form = UserForm(instance=request.user)
        # …

  • How to dataframe a nested json in python? The json is recursive has same key names that need to be dataframed as a same column

    1 day ago

    Try:

    dct = {
        "employee": [
            {
                "company": "xxx",
                "address": "yyy",
                "person": [
                    {
                        "name": "myname",
                        "qualifications": "graduate",
                        "person": [
                            {
                                "name": "myname2",
                                "qualifications": "graduate2",
                                "person": [
                                    {
                                        "name": "myname3",
                                        "qualifications": "graduate3",
                                    }
                                ],
                            }
                        ],
                    }
                ],
            }
        ]
    }
    
    
    def get_employees(d, company=None, address=None):
        if isinstance(d, dict):
            company = d.get("company", company)
            address = d.get("address", address)
    
            if "name" in d:
                yield company, address, d["name"], d.get("qualifications")
    
            yield from get_employees(d.get("person"), company, address)
        elif isinstance(d, list):
            for v in d:
                yield from get_employees(v, company, address)
    
    
    df = pd.DataFrame(
        get_employees(dct["employee"]),
        columns=["company", "address", "name", "qualifications"],
    )
    print(df)
    

    Prints:

      company address     name qualifications
    0     xxx     yyy   myname       graduate
    1     xxx     yyy  myname2      graduate2
    2     xxx     yyy  myname3      graduate3
    

  • Modify .bash_aliases with Python

    1 day ago

    I can't find a way to determine what user ran the script to modify their file.

    You can reference the file ~/.bash_aliases in your script and run it without sudo, unless your current user is root.

    EDIT: You simply need to add write privileges to .bash_aliases for every user it belongs to.

  • How to use lambda by having inputs in python?

    1 day ago

    I guess you can do this ... but you probably should not (see PEP8 )

    # def d_a(): < you dont need(or want) this line ...
    d_a_compute = lambda v_i, t, a: (v_i * t) + (0.5 * a) * (t ** 2)
    ...
    if choice == "d_a":
       result = d_a_compute( # <- you need to actually call you lambda
         float(input("Enter the initial velocity: ")), # v_i
         float(input("Enter the time: ")),  # t
         float(input("Enter the acceleration: ")) # a
       )
       print("DA = ", result)
    

    as pointed out in the comments its almost certainly more readable to just use a normal function here (in fact it is explicity called out in PEP8 that you should use a normal function definition rather than assign a lambda to a variable)

      def d_a_compute(v_i, t, a):
          return  (v_i * t) + (0.5 * a) * (t ** 2)
    

    instead

  • Converting struct headers to and from vec&lt;char&gt;

    1 day ago

    With every iteration, you increment step by the size of the current packet. It is an index into stream of where the next packet starts. However, you add to bitr this index, resulting in Undefined Behavior because bitr has been increased past the end of the buffer.

    What you get is:

    start of 1st loop: bitr = 0
    start of 2nd loop: bitr = 51 (size of first packet)
    start of 3rd loop: bitr = 139 (51 + 51 + 37, 2 * size of first packet + size of second packet)
    

    What you want to do is increment bitr the same amount you increase step (bitr+=header.len + sizeof(header_t);), or set set bitr to the newly computed index (bitr = &stream[step];).

  • Snowflake Java UDF can&#39;t return an array of floats?

    2 days ago

    Check the docs for allowed types: float is ok, but `Float is unsupported.

    Therefore, this is the code fixed:

    create or replace function float_array_test()
    returns array
    language java
    handler='MyClass.test'
    as
    $$
        class MyClass {
            public static float[] test() {
                return new float[] {(float)1.1, (float)2.2};
            }
        }
    $$;
    

    But beware of the error incompatible types: double cannot be converted to Float, that's why I had to cast the hard-coded doubles to float.

    An even better solution is to use doubles in Java land, which will be converted to floats in Snowflake land without precision loss:

    create or replace function float_array_test()
    returns array
    language java
    handler='MyClass.test'
    as
    $$
        class MyClass {
            public static double[] test() {
                return new double[] {1.1, 2.2};
            }
        }
    $$;
    

  • Python using nested list comprehensions to create a accelerated increase

    2 days ago

    This should behave the same way your for loop does.

    numbers = [x * (i+1) for x, i in zip(range(50), range(50))]
    

    or

    numbers = [x * i for x, i in zip(range(50), range(1, 51))]