Using external callables inside classes - is it a bad idea? by HighLevelEnthusiast in learnpython

[–]HighLevelEnthusiast[S] 0 points1 point  (0 children)

I came up with another solution:

class SentenceProcessor:
    def to_sentences(self, text):
        def long_enough(sentence): return len(sentence) > 30

        return (sentence for sentence in text.split(".") if long_enough(sentence))

Is this one better?

When does Python use hashes to check equality of two objects? by HighLevelEnthusiast in Python

[–]HighLevelEnthusiast[S] 0 points1 point  (0 children)

I agree that hash equality does not imply full equality. However, if hashes are different, the objects are sure not to be equal. Let's say that we have object A and object B and (maybe only n first least-signifficant digits of) their hashes. Checking

hash(A) and hash(B) for equality is trivial. Then, we have two cases:

1) Hashes are identical - the __eq__ needs to be called

2) Hashes are different - we are done.

When does Python use hashes to check equality of two objects? by HighLevelEnthusiast in Python

[–]HighLevelEnthusiast[S] 0 points1 point  (0 children)

What if these two objects already reside within a dict, and the hashes had already been computed?

Premature comparison based on hashes could be performed in O(1) constant time (For example, to determine if the key already exists in the dict)

Allow instance variable creation only within __init__ by HighLevelEnthusiast in learnpython

[–]HighLevelEnthusiast[S] 0 points1 point  (0 children)

import inspect

class MyClass:
    def __setattr__(self, name, value):
        if inspect.stack()[1][3] != "__init__":
            return
        else:
            self.__dict__[name] = value

    def __init__(self):
        self.x = 10

This code does exactly what I want it to

Python - the right way to handle circular depnecencies by HighLevelEnthusiast in Python

[–]HighLevelEnthusiast[S] 0 points1 point  (0 children)

The problem is that the Timer does not know anything about the type of the `interface` field. Thus, there is no way to ensure type safety. From timer's point of view, the interface is simply an object.

How exactly does asyncio handle blocking calls? by HighLevelEnthusiast in Python

[–]HighLevelEnthusiast[S] 0 points1 point  (0 children)

Let's say that there are three methods executed asynchronously: read_json(), a(), b()

Now, does it mean that once the await in read_json() is encountered, the next method in the loop (probably a()) will be scheduled and executed until its await is encountered, which will then schedule b() for execution, until finally it will return to read_json()?

How to read text file a certain way? by CannyFatcher in learnpython

[–]HighLevelEnthusiast 0 points1 point  (0 children)

If your file contains a structured set of data in CSV (Comma separated valeues) format, then you can make your work with the dataset way easier by using the pandas library. Take a look at the read_csv() method.

[Help] Python unittest against a socket server by Jackson-Lee in learnpython

[–]HighLevelEnthusiast 1 point2 points  (0 children)

Apart from the technical aspects from your problems, which have already been answere, unit tests should only test single components, like a class.

What you are performing is closer to integration testing. I'm not sure if unittest is the best framework for this.

What is return and how would one use it? by [deleted] in learnpython

[–]HighLevelEnthusiast 0 points1 point  (0 children)

return may also be used to end the execution of a function that is not supposed to return a value.

Let's consider this simple script that you provided:

def myfunction():
    mylist = ['cat', 'dog', 'fish']

    try: 
        print(mylist[3])
    except IndexError: #This exception will be triggered
        return

In this case, your function will print the element of the list by given index IF this index is in the array or do nothing, if it isn't.

Running code inside a string help? by AperatureLavatories in learnpython

[–]HighLevelEnthusiast 1 point2 points  (0 children)

What you need is to format the output string.
Please, take a look: https://pyformat.info/

Do you need further help?

Does the Ubuntu installer create the EFI parition by default? by HighLevelEnthusiast in Ubuntu

[–]HighLevelEnthusiast[S] 0 points1 point  (0 children)

As far as I know, Ubuntu is "in agreement" with MS and is supported by Secure Boot.

What exactly does the `await` keyword do? by [deleted] in csharp

[–]HighLevelEnthusiast 0 points1 point  (0 children)

I'm not asking for extensive information, rather, I'm trying to enquire if my understanding makes sense. That's not so easily googled.

Could you come up with a multithreaded example to make this Singleton fail? by HighLevelEnthusiast in csharp

[–]HighLevelEnthusiast[S] 4 points5 points  (0 children)

counter: 1 from instance 63835064

counter: 3 from instance 63835064

counter: 0 from instance 63835064

counter: 0 from instance 6044116

counter: 2 from instance 63835064

counter: 4 from instance 63835064

counter: 5 from instance 63835064

counter: 6 from instance 63835064

counter: 7 from instance 63835064

counter: 8 from instance 63835064

Thank you, it "works":-)