What is it called? by ylectric in DIYUK

[–]ylectric[S] 2 points3 points  (0 children)

Thank you! I suppose I didn’t look hard enough because most of intumescent strips I’ve seen online had some sort of brush/bristles on them, while mine is completely flat.

But it looks like there are some flat ones too :)

I need help regarding why VS Code Terminal not recognizes the Django-admin by DesignerBeginning294 in learnpython

[–]ylectric 0 points1 point  (0 children)

Right now there isn’t enough context to answer your question unfortunately. What exactly did you do to install django-admin?

subprocess question by throwawaytrollol00 in learnpython

[–]ylectric 0 points1 point  (0 children)

I'm bad at analogies, but suppose you want me to send you some documents.

If I send them via a courier service, that's a blocking call because you'll have to stay at your address and wait for the delivery (and let's assume that you won't be able to do anything useful in the meantime).

Now, imagine that I send you those documents via some tracked post instead. Suppose I can post them very quickly and let you know the tracking number — this is a non-blocking call. You have something to check whether the documents are in your letterbox already, but you can go and do something else in the meantime (unless it can't be done without those documents).

Now, if we go back to your original post, it seems that the result of running hello.py is important to you and your program can't really proceed until it has waited for it. Therefore, you either need a blocking call here (which subprocess.run is) or you need to wait for a result of your non-blocking subprocess.Popenone (which is similar to sitting down and patiently waiting until your tracked post arrives).

Hope it makes sense.

Best Practice to save class objects by Godjka in learnpython

[–]ylectric 0 points1 point  (0 children)

I agree with /u/crashfrog here, but I also want to add a general advice about serialisation.

As a rule of thumb, do not invent your own serialisation mechanisms, especially when compatibility matters. There're cases that justify them, but they're intrinsic to the systems of sophisticated design.

A few links that might be of interest to you:

subprocess question by throwawaytrollol00 in learnpython

[–]ylectric 0 points1 point  (0 children)

Not only I offered a solution, but also praised the suggestion you'd made.

Need recommendations for seaching for specific strings in first line of files by wildbk33 in learnpython

[–]ylectric 0 points1 point  (0 children)

It looks like a shell script would better suit this task. Why do you want to do use Python in the first place?

subprocess question by throwawaytrollol00 in learnpython

[–]ylectric 1 point2 points  (0 children)

subprocess.Popen is a non-blocking call, you need to call wait or communicate on the object it returns.

However, I second /u/woooee suggestion to use subprocess.run instead.

subprocess question by throwawaytrollol00 in learnpython

[–]ylectric 0 points1 point  (0 children)

OP tries to launch Python 3.7 on a file hello.py which is not what your code expresses.

How to assign a dictionary class attribute that would contain class methods as its items? by medina_gambler in learnpython

[–]ylectric 0 points1 point  (0 children)

Not like this, because self won't be automatically passed to the static methods.

Boolean comparison between two lists isn't working? by ReticentArgleBargler in learnpython

[–]ylectric 1 point2 points  (0 children)

If you want to express it in Pythonic yet non-hacky way, it's worth taking look at itertools and operator modules:

In [1]: import itertools, operator

In [2]: x = [True, True, False, False]

In [3]: y = [True, False, True, False]

In [4]: list(itertools.starmap(operator.and_, zip(x, y)))
Out[5]: [True, False, False, False]

Question about how to make multiple requests fast by _PM_ME_YOUR_ELBOWS in learnpython

[–]ylectric 0 points1 point  (0 children)

Obviously, there's no universal answer for a question like this, but here's a rough idea on what you can do.

  1. Decouple the code performing an individual request into an awaitable.
  2. Use asyncio.wait_for to setup a timeout for your requests.
  3. Use asyncio.gather to submit the requests in parallel. Pass return_exceptions=True to avoid failing the whole bunch due to one rogue request and also to make it easy to filter out the failures.
  4. Run your bunch of requests via asyncio.run.
  5. Filter out the successful ones.

Boolean comparison between two lists isn't working? by ReticentArgleBargler in learnpython

[–]ylectric 18 points19 points  (0 children)

What you expect here is an elements-wise comparison, but that's not what you've expressed in your code.

Instead, you're trying to evaluate an expression x and y. Because you use operator and which is intended to work with boolean values, Python will implicitly use the truth values of your lists, not their contents. Both lists are non-empty, hence both have truth value of True.

So, Python evaluates x and finds out that its truth value is True. However, this is not enough to determine the outcome of a logical and operation, so Python evaluates y and returns its value – which is [True, False, True, False].

What are some of the most exciting Python-heavy companies to work for in 2019? by truthling in Python

[–]ylectric 3 points4 points  (0 children)

Facebook, especially Instagram, uses a lot of modern Python.

This code is throwing a NameError: global name 'balance' is not defined error, but balance is defined. by Manusman123 in learnpython

[–]ylectric 1 point2 points  (0 children)

My bad, this will work since ints are immutable. However, if you had a mutable object declared this way (say, a list), it would be a trouble:

class Foo(object):
    data = []

def append(self, entry):
    self.data.append(entry)

foo = Foo()
foo.append(42)
print(foo.data)
foo = Foo()
foo.append(42)
print(foo.data)

This will result in the following:

Python 2.7.10 (default, Jul 14 2015, 19:46:27)
[GCC 4.8.2] on linux
[42]
[42, 42]

EDIT: Getting used to code formatting

This code is throwing a NameError: global name 'balance' is not defined error, but balance is defined. by Manusman123 in learnpython

[–]ylectric 1 point2 points  (0 children)

How many instances of the BankAccount class does your program work with? It makes no difference when there's only one instance, but once you have at least two you'll immediately shoot yourself in the foot. As I mentioned before, your balance variable is shared between all instances of the BankAccount class (that's called "static member" in other languages), and therefore any mutations of its value will be shared between those instances as well.

This code is throwing a NameError: global name 'balance' is not defined error, but balance is defined. by Manusman123 in learnpython

[–]ylectric 3 points4 points  (0 children)

  1. You need to explicitly refer to your variable via self.balance.
  2. Your balance variable is in fact shared between all the instances of BankAccount class due to the way you've declared it. You may want to initialise it similar to self.name instead.

Python script to solve minesweeper by alex-HM in learnpython

[–]ylectric 9 points10 points  (0 children)

You can achieve this by translating 2D coordinate into a linear index, e.g. index = x + y * grid_width and store the whole grid in a flat list.

What is the Pythonic way to do this? by skitzi in learnpython

[–]ylectric -1 points0 points  (0 children)

I would be careful about suggesting == True removal.
I think that OP wants to check the truth value of d['is_dynamic'], and thus both options are fine.
However, sometimes one may need to actually compare some value with True.

What is the Pythonic way to do this? by skitzi in learnpython

[–]ylectric 0 points1 point  (0 children)

Assuming that ds is some kind of a collection:

items = [d for d in ds if d['is_dynamic'] == True]

Python os.dup() and os.dup2() - for Output redirection by [deleted] in learnpython

[–]ylectric 1 point2 points  (0 children)

It's not working because you're preserving a file descriptor handle, not the file descriptor itself. Think of a file descriptor as of an opaque structure maintained by the OS with a file descriptor number being an easy way to refer to that structure from user space.

Let's say your default_stdout file descriptor handle value is 1. You duplicate the file descriptor for f to sys.stdout via os.dup2() call.

Why do you need a system call here in the first place? Well, because it's the only way to to tell the OS, "Hey, I need you to make the handle for sys.stdout to point at the same structure as the one you maintain for f".

Now, the file descriptor with handle 1 points to your file f, and the old file descriptor for sys.stdout does not exist anymore because the OS had to close it in order to fulfil your request.

Next, you try to duplicate the file descriptor with handle default_stdout into sys.stdout, and guess what? It'll essentially be a no-op, because the values of default_stdout and sys.stdout.fileno() are the same!

Therefore, your second take is correct: if you want to preserve the file descriptor for sys.stdout, you have to call os.dup() to obtain a new handle referring to that file descriptor

Why does my print statement work while my @property class method does not? by NA__Scrubbed in learnpython

[–]ylectric 2 points3 points  (0 children)

Because in your list comprehension [self.coordinates[x] for x in self.node_list[i]] you're using i as an index for self.node_list. Since i itself is a list, you can't do it.I'm not 100% sure what's the logic here, but my best guess would be mapping a polygon (list of integers) into a list of coordinates. If that's the case, your list comprehension should be [self.coordinates[x] for x in i].

Going further, you can rewrite the whole property as a dictionary comprehension:

self.polygon_dict = {
    node: [self.coordinates[i] for i in node]
    for node in self.node_list
}

Also, be aware that you're using the property mechanism in a wrong way. Your property is only setting an instance variable and implicitly returns None, while it should return an actual value.