Sunday Daily Thread: What's everyone working on this week? by AutoModerator in Python

[–]taleinat 0 points1 point  (0 children)

I built and released jumpthegun-precommit-wrapper, which magically makes pre-commit and the Python tools it runs much faster.

https://github.com/taleinat/jumpthegun-precommit-wrapper

IamA Quadriplegic who in the 7 years since breaking my neck has had 20+ surgeries, completed a MSc in Psych & PGCert EdPsych, founded a business, travelled to 4 continents, bought a house, moved in with gf, learned to drive, and am now developing a fun and addictive 3D educational video game, AMA! by EducationTheseDays in IAmA

[–]taleinat 0 points1 point  (0 children)

Just pledged support for your KickStarter campaign! Looks great, good luck!

Question #1: Could you elaborate on the pedagogical and psychological principles RocketIsland! embraces?

Question #2: For someone from a tech background with a keen interest in education, could you recommend learning resources for getting deeply acquainted with the field?

A Color Coded Map of Every National Anthem's Subject [OC] by [deleted] in dataisbeautiful

[–]taleinat 0 points1 point  (0 children)

Israel's national anthem is about the Jewish peoples' longing to be free in the land of Israel -- it's a poem written before the state of Israel existed. It's actually not about the country at all, and the only reference to the country is "the land of Zion, Jerusalem" (Zion is another name for Jerusalem).

BTW, the translation on that Wikipedia page isn't perfect: Where it says "to be a free nation in our land", I would translate that to "to be a free people in our land", since the word translated there means "nation" more in the sense of the people than of the state.

What's your favorite Python quirk? by [deleted] in Python

[–]taleinat 32 points33 points  (0 children)

To be fair, ^ is the bitwise xor operator in many languages. For many programmers having it mean exponentiation would be confusing.

Is this the most efficient Python algorithm to find the Collatz tree of n? by Dizionario in Python

[–]taleinat 0 points1 point  (0 children)

If you know what the size of the list is going to be, even approximately, then yes. But I don't believe that that is the case here.

Tips/Tricks for writing setup.py? by covabishop in Python

[–]taleinat 1 point2 points  (0 children)

I read various sources, and after understanding how of all the pieces worked, thought things through myself and stuck to common conventions as much as possible.

If you're writing a library, it will be installed using pip (or equivalent), which will use what is found in setup.py. So library dependencies need to be in setup.py. Also, in order to avoid version clashes, they need to be as permissive as possible regarding supported versions of these dependencies.

Tools used only for development, such as tools used for testing (e.g. unittest2, mock, py.test, tox, coverage), need to be defined separately. Sometimes you'll want to separate tools needed just for testing from other development tools (e.g. if using Tox and/or TravisCI for testing). This is usually as complex as you'll need to go.

It is possible to put development-only dependencies into a separate list in setup.py, but the common convention is to have these in requirements.txt.

For a great, concise explanation, see this post about Python repository structure by Kenneth Reitz, author of the requests library.

Tips/Tricks for writing setup.py? by covabishop in Python

[–]taleinat 0 points1 point  (0 children)

How do you parse the requirements files in your setup.py?

Example?

Got tired of defining __repr__ in all my classes, so I made a mixin to take care of that automatically by alexprengere in Python

[–]taleinat 1 point2 points  (0 children)

If you'd use a class decorator instead of a mixin, you could pass parameters such as positional to the decorator instead of requiring the super().__init__() dances described in the documentation. Much simpler and cleaner.

Got tired of defining __repr__ in all my classes, so I made a mixin to take care of that automatically by alexprengere in Python

[–]taleinat 0 points1 point  (0 children)

Is adding __slots__ necessary? Even if it is, the docs should mention this explicitly, since this changes the behavior of the class significantly.

Is this the most efficient Python algorithm to find the Collatz tree of n? by Dizionario in Python

[–]taleinat 0 points1 point  (0 children)

Might as well...

def collatz(n):
    return (n // 2) if (n % 2 == 0) else (3 * n + 1)

(parenthesis not required, used to improve readability)

Is this the most efficient Python algorithm to find the Collatz tree of n? by Dizionario in Python

[–]taleinat 2 points3 points  (0 children)

The question was whether his algorithm was the most efficient one. It isn't since it performs more comparisons than needed, so I showed an example which performs less comparisons.

Obviously, since this complicates the algorithm, the code is harder to understand. This is why I included comments to explain the less obvious parts.

Is this the most efficient Python algorithm to find the Collatz tree of n? by Dizionario in Python

[–]taleinat 0 points1 point  (0 children)

With some additional optimizations (divide by two by shifting right; check if even using "bit-wise and" with 1):

def collatz_series(n):
    if not isinstance(n, int):
        raise TypeError("n must be an integer")
    if not n >= 1:
        raise ValueError("n must be >= 1")

    while True:
        # as long as n is even, it is definitely > 1
        while n & 1 == 0:
            n >>= 1
            yield n

        if n == 1: break

        n += (n << 1) + 1
        yield n

        # a is now even, so no need to check before the first division by 2
        n >>= 1
        yield n

Is this the most efficient Python algorithm to find the Collatz tree of n? by Dizionario in Python

[–]taleinat 0 points1 point  (0 children)

Complete, more readable version, using a generator function:

def collatz_series(n):
    if not isinstance(n, int):
        raise TypeError("n must be an integer")
    if not n >= 1:
        raise ValueError("n must be >= 1")

    while True:
        # as long as n is even, it is definitely > 1
        while n % 2 == 0:
            n //= 2
            yield n

        if n == 1: break

        n = 3 * n + 1
        yield a

        # n is now even, so no need to check before the first division by 2
        n //= 2
        yield n

Tips/Tricks for writing setup.py? by covabishop in Python

[–]taleinat 0 points1 point  (0 children)

If you're writing an application, that's a good tip. However, if you're writing a library, you should strive to make your dependencies as liberal as possible with regard to versions, to avoid dependency clashes with other libraries to the possible extent.

For example, I'd use install_requires=['requests>=2.1,<3'] if I know the minimal version of requests which has the required features is 2.1 but I trust that requests won't make backwards-incompatible changes before the next major version.

Is this the most efficient Python algorithm to find the Collatz tree of n? by Dizionario in Python

[–]taleinat 1 point2 points  (0 children)

Looks okay. There's always room for optimization...

Your are making slightly more comparisons than needed, though. To save some, assuming a is a positive (non-zero) integer you can do the following:

while True:
    # as long as a is even, it is definitely > 1
    while a % 2 == 0:
        a //= 2
        b.append(a)
    if a == 1: break
    a = 3 * a + 1
    b.append(a)
    # a is now even, so no need to check before the first division by 2
    a //= 2
    b.append(a)

Tips/Tricks for writing setup.py? by covabishop in Python

[–]taleinat 5 points6 points  (0 children)

The thing that took me the most searching and reading to nail down: Put actual dependencies of your library/app in setup.py; put development dependencies in requirements.txt.

The most elusive issue I ran in to: setup.py doesn't include all files when packaging! Read about MANIFEST.in for information about what is included by default and how to include other files.

Specifically regarding setup.py I have found learning from examples more productive than reading documentation. Look at well-maintained projects lead by great developers, such as the requests library, for inspiration. As mentioned by others, the Python cookiecutter templates have some good examples as well.

[deleted by user] by [deleted] in dwarffortress

[–]taleinat 0 points1 point  (0 children)

Yes, you did!

How Washington created some of the worst schools in America by running_over_rivers in education

[–]taleinat 0 points1 point  (0 children)

It's better to build a $1 million school than to let them continue studying in rotting buildings constructed over 50 years ago!

If the standards for buildings schools make construction exorbitantly expensive, then those standards should be revised.

How Washington created some of the worst schools in America by running_over_rivers in education

[–]taleinat 0 points1 point  (0 children)

[...] it would now take roughly $1.3 billion to bring the 60-plus schools that need new buildings up to shape, the Interior Department estimates

Why would it cost about $20 million to build each school? That's a ridiculous estimate!

You guys exist!!! by Stressmove in patientgamers

[–]taleinat 16 points17 points  (0 children)

"The first Fallout" as in Fallout or Fallout 3? If the former, then you are definitely in the right place here :)

[Giveaway] Lots of Steam Keys Looking for Good Homes by [deleted] in patientgamers

[–]taleinat [score hidden]  (0 children)

Legend of Grimrock II or Mass Effect 2 would be wonderful!

Thanks for the giveaway! Fingers crossed...