python 3.x – Suggestion regarding fixing the seed through all the modules of a project

I am building a project and I have some parameters which I want to fix through all the modules of the project.
Below are the parameters –

# Deterministic Behaviour
seed = 1
os.environ('PYTHONHASHSEED') = str(seed)
## Torch RNG
## Python RNG

## CuDNN determinsim
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False

Now, I want to fix the same parameters like random, seed through all the files. One solution is to write the above code in every file but that is not very modular and clean. Is there a suggestive way to do that?

My directory structure looks like below –

├── Combined_Files_without_label5.csv
├── __pycache__
│   ├── dataset.cpython-37.pyc
│   ├── datasets.cpython-37.pyc
│   └── testing.cpython-37.pyc
├── dataset.ipynb
├── import_packages
│   ├──
│   ├── __pycache__
│   │   ├── __init__.cpython-37.pyc
│   │   ├── dataset.cpython-37.pyc
│   │   ├── dataset_class.cpython-37.pyc
│   │   ├── dataset_partition.cpython-37.pyc
│   │   └── visualising.cpython-37.pyc
│   ├──
│   ├──
│   └──
└── model.ipynb

asynchronously extract title from urls using python

The following code asynchronously extract title from urls (saved in book-urls.txt).

from bs4 import BeautifulSoup
import grequests

links = list()

file1 = open("book-urls.txt", "r")
Lines = file1.readlines()
for line in Lines:

reqs = (grequests.get(link) for link in links)
resp =

for r in resp:
    soup = BeautifulSoup(r.text)

The input is:

The output is:

A Reviewer's Handbook to Business Valuation: Practical Guidance to the Use and Abuse of a Business Appraisal (Book)
Accounting All-in-One For Dummies, with Online Practice, 2nd Edition (Book)
Business Valuation For Dummies (Book)
The Business of Value Investing: Six Essential Elements to Buying Companies Like Warren Buffett (Book)

is grequests the best option or should I use something else?

Please give some suggestion on how I can make this code better.

python – Joining lines together into a single line

I’ve been working on a GDscript to draw isolines (contours). I’m using the Meandering Triangles algorithm from, and it works pretty well! The problem is I don’t know how to do Step 4, which is where all the little individual lines that make up the contour lines are joined together, so that I have a set of points describing an entire isoline which I can then drop into a Line2D.points.

Bruce wrote his code in Python, which I’ve yet to learn, and I just don’t quite get how the joining works under the hood. That is, I understand the principle—find a segment which touches the current segment, and join them together, then do it again and again—but I don’t understand how the code makes that happen. And thus I haven’t been able figure out how to make it work in GDscript, let alone do so performantly. I feel like I really should do this, as the current implementation (where all the lines are still their own Line2D nodes creates 3,000-5,000 such nodes. Turning those into, say, a couple dozen Line2Ds really feels like a good idea.

Below is the code the generates the several thousand tiny lines; apologies if I wrote awful code (looking at you, var offset) but I’m only two weeks into GDscript and, as I said, no weeks into Python. cols and rows are defined earlier in the script, and are at present in the 50-100 range.
Any help will be greatly appreciated!

func contourDraw(isorange = Vector3(0,0,0), xmin = 0, xmax = cols, ymin = 0, ymax = rows):
    var triangles = ()
    for x in range(xmin, xmax - 1):
        for y in range(ymin, ymax - 1):
            triangles.append((Vector2(x,y), Vector2(x+1,y), Vector2(x,y+1)))
            triangles.append((Vector2(x+1,y), Vector2(x,y+1), Vector2(x+1,y+1)))

    var contour_segments = ()
    for triangle in triangles:
        for isocheck in range(isorange.x, floor(isorange.y /isorange.z)):
            var isoline = isocheck * isorange.z
            var below = ()
            var above = ()
            for vertex in triangle:
                if matrix(vertex.x)(vertex.y).strength < isoline: below.append(vertex)
                if matrix(vertex.x)(vertex.y).strength >= isoline: above.append(vertex)
            if below.size() == 0 or above.size() == 0:
            var minority = above if above.size() < below.size() else below
            var majority = above if above.size() > below.size() else below
            var contour_points = ()
            var crossed_edges = ((minority(0), majority(0)), (minority(0), majority(1)))
            for triangle_edge in crossed_edges:
                var offset = ( ( isoline - matrix(triangle_edge(1).x)(triangle_edge(1).y).strength ) / ( matrix(triangle_edge(0).x)(triangle_edge(0).y).strength - matrix(triangle_edge(1).x)(triangle_edge(1).y).strength ) )
                var crossing_point = Vector2(
                    (offset * triangle_edge(0)(0)) + ((1 - offset) * triangle_edge(1)(0)),
                    (offset * triangle_edge(0)(1)) + ((1 - offset) * triangle_edge(1)(1))
                contour_points.append(crossing_point * grid_space)

    var counter = 0
    for segment in contour_segments:
        var line =
        line.default_color = Color.from_hsv(0.58,0.5,0.5)
        line.width = 1
        line.antialiased = true
        line.points = segment
        counter += 1

python – Cual es la funcion del booleano TRUE dentro de functools en este ejemplo

Tengo esta pequeña duda.
Quiero comparar 2 listas y tengo este ejemplo :

import functools 
l1 = (10, 30, 45, 50) 
l2 = (10, 30, 50, 40, 70) 
l3 = (10, 30, 45, 50) 
if functools.reduce(lambda x, y : x and y, map(lambda p, q: p == q,l1,l2),True): 
    print ("Las listas l1 y l2 son las mismas") 
    print ("Las listas l1 y l2 no son las mismas")

El resultado lo da bien (si comparo l1 y l2 me dice que NO son iguales, si comparo l1 y l3 me dice que SI son iguales)

La duda es que hace el TRUE metido adentro del metodo functools.reduce.

Buscando la documentacion no aparece ese boleano como argumento de functools :

Es mas , cuando pruebo el codigo sin ese TRUE me da la respuesta buena igual.

Que hace ese TRUE alli, y como es posible que no de error , si no es un argumento de functools.reduce ?

Muchas gracias.

python – Pattern for creating an instance of a class from a UI form

The following is a contrived example of a potential code smell
that I’ve repeatedly encountered when implementing a GUI with which instances of a class created by letting the user fill out a form. The question is if the pattern I use is correct, or if there is a better way.

The app’s core logic contains a class Data that is initialized with
a Dict (among other things):

class Data:
    def __init__(self, stuff: Dict, *args, **kwargs) -> None:
        self.stuff = stuff
        # ...

The attribute self.things is read-only. Note that __init__ has other arguments,
which are used to set other attributes of Data.

The main use-case is that raw is read from a .json file:

with open(path, 'r') as f:
    raw =
stuff = json.loads(raw)
data = Data(stuff, ...)

Instances of Data may be created using a UI form,
in which the user enters a filesystem path (from which raw is read)
and the other parameters of __init__.
The instances should also be managed by the app’s UI; when the app is closed, the
Data instances should be serialized, saved, and later loaded again.
Out of the potentially very large database of Data objects,
only very few are required at the same time.

To prevent massive data duplication (let’s say the file at path is
very large, and potentially used by many Data objects) and to allow
editing the Data instances by changing the path that the raw
parameter is read from, the file path should be saved, not the data

This means that the Data instances themselves cannot be serialized.
Instead, I’ve opted to use an intermediate class

class DataInfo:
    def __init__(self, path: str, *args, **kwargs) -> None:
        self.path = path
        # Store everything in attributes...

    def deploy(self) -> Data:
        with open(path, 'r') as f:
            raw =
        stuff = json.loads(raw)
        data = Data(stuff, ...)
        return data

The instances of this class are used to store the initialization args
for Data, and on DataInfo instances are serialized/loaded.
Once the Data object is actually required (remember that
only very few of the instances are required at each moment), it is
constructed by calling deploy.

Maybe I’m misjudging this, but this pattern feels slightly awkward. Is this (roughly) the correct pattern for this situation? Does it have a name? Or are there better solutions?

Parse python code, for specific pep8 issues

I am aware, that libraries exist for parsing python code, however, for the sake of learning how they parse errors, I’m creating a script that checks a file for only 6 Pep8 errors just for reference.

This is how my current 6 Pep8 functions look (they append an issue to the issues list, if issue was found)

(S001) Line is longer than 79 characters
(S002) Indentation is not a multiple of four
(S003) Unnecessary semicolon after a statement (note, semicolons are admissible in comments)
(S004) At least two spaces before inline comments required
(S005) TODO found (only in comments; the case does not matter)
(S006) More than two blank lines used before this line (must be output for the first non-empty line)
def S001(self, ln_num: int, line: str):
    if len(line) > 79:
        self.issues.append(f"Line {ln_num}: S001 Too Long")

def S002(self, ln_num: int, line: str):
    indentation_length = len(line) - len(line.lstrip())
    if indentation_length % 4 != 0:
        self.issues.append(f"Line {ln_num}: S002 Indentation is not a multiple of four")

def S003(self, ln_num: int, line: str):
    regex1 = re.compile("(.*)((;(s)*#)|(;$))")
    regex2 = re.compile("#.*;")
    if and not
        self.issues.append(f"Line {ln_num}: S003 Unnecessary semicolon")

def S004(self, ln_num: int, line: str):
    regex = re.compile("(((^ ){2})|(s(^ ))|((^ )s))#")
        self.issues.append(f"Line {ln_num}: S004 At least two spaces before inline comments required")

def S005(self, ln_num: int, line: str):
    regex =  re.compile("#(.*)todo", flags=re.IGNORECASE)
        self.issues.append(f"Line {ln_num}: S005 TODO found")

def S006(self, ln_num: int, line: str):
    if self.code(ln_num-4:ln_num-1) == ('', '', '') and line != "":
        self.issues.append(f"Line {ln_num}: S006 More than two blank lines used before this line")


""" Test case 1 """
print('What's your name?') # reading an input
name = input();
print(f'Hello, {name}');  # here is an obvious comment: this prints greeting with a name

very_big_number = 11_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000

def some_fun():
    print('NO TODO HERE;;')
    pass; # Todo something
""" END """

""" Test Case 2 """
print('hello');  # hello
# hello hello hello;
greeting = 'hello;'
print('hello')  # ;
""" END """

""" Test Case 3 """
    print('hello')  # TODO
    print('hello')  # TODO # TODO
    # todo
    # TODO just do it
    print('TODO TODO')
    todo = 'todo'
""" END """

""" Test Case 4 """


""" END """

""" Test Case 5 """
# just a comment
print('hello!')  #
print('hello!')  # hello

print('hello!') # hello
print('hello!')# hello
""" END """

Testcase 1 Expected Output:

Line 1: S004 At least two spaces before inline comment required
Line 2: S003 Unnecessary semicolon
Line 3: S001 Too long
Line 3: S003 Unnecessary semicolon
Line 6: S001 Too long
Line 11: S006 More than two blank lines used before this line
Line 13: S003 Unnecessary semicolon
Line 13: S004 At least two spaces before inline comment required
Line 13: S005 TODO found

I am aware my code, is not optimal, and doesn’t satisfy every edge case, but I want an idea, on how they parse the errors properly. I would like improvements, or better ideas on how to parse for errors, since i personally don’t like my answers.

Python error (custom windows live messenger server)

could someone help me i’m getting this issue
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such column: t_user.contacts

(SQL: SELECT AS t_user_id, t_user.date_created AS t_user_date_created, t_user.date_login AS t_user_date_login, t_user.uuid AS t_user_uuid, AS t_user_email, t_user.verified AS t_user_verified, AS t_user_name, t_user.message AS t_user_message, t_user.password AS t_user_password, t_user.settings AS t_user_settings, t_user.groups AS t_user_groups, t_user.contacts AS t_user_contacts, t_user.type AS t_user_type, t_user.password_md5 AS t_user_password_md5

FROM t_user

WHERE lower( = ?)

(parameters: (‘’,))

(Background on this error at:

design patterns – Testing lower- and upper bound values in Python

Except under unusual circumstances, classes are things or entities — hence the
term objects — while functions or methods are actions or operations. You
want to name them according. For that reason Get_labels strikes me as an
oddly named class. Based on what you’ve shown us, I might suggest the name
Bounds as an alternative. A side benefit of that name is that it allows you
to shorten up the attribute names without loss of meaning.

A separate method for checking the basic validity of the bounds seems like
over-engineering to me — unless the checking logic becomes much more complex
or unless it will be used elsewhere in the code. So I would just do simple
validation in __init__() in this case.

Avoid the temptation to use chatty or verbose message in your code. It won’t
help you or your users over the long run — at least that’s my experience. Keep
things direct and rigorously concise. In fact, it’s often beneficial to keep
the messages very technical rather than natural in their stylistic orientation.
What I mean by that is rather than describing the problem the way one might
verbally to a human (“The lower bound, which was 1000, must be less than the
upper bound, which as 125”), you are often better off to describe the problem
in a formulaic, schematic, computer-like way. Among other things, that approach
allows you to adopt a conventional format for all error messages in an
application. The error message format shown in the rewrite below could be
described generically as PROBLEM: SELF. A consistent approach makes it
easier to write validation code in the first place and to maintain it over
time. Consistency also conveys professionalism to users.

Along those lines, you can often simplify the creation of such validation
messages by first defining __repr__() for your class, as illustrated below.

For the validations you have so far, a ValueError is a closer
fit than raising a general Exception. Also, you might consider checking
for other kinds of errors: for example, are bounds restricted to integers only?
If you do check for that, raise a TypeError.

Finally, a stylistic and admittedly subjective fine point. Below is a stack
trace from your code as written. We see the verbose message twice, first as an
f-string and then with parameters filled in. What’s wrong with that? Nothing
serious, but it’s heavy, tedious, even lacking a certain elegance. At a
minimum, one could say that the repetition of the verbose message is mildly
distracting to the user, putting an extra increment of visual or cognitive
burden on the user to figure out what’s going on. Compare that to the stack
trace from the revised code.


Traceback (most recent call last):
  File "", line 19, in <module>
    get_labels = Get_labels(-1,25,"first")
  File "", line 7, in __init__
  File "", line 17, in check_threshold_validity
    raise Exception(f'Sorry, the lower threshold={self.lower_bound} should be larger than 0 for configuration={self.configuration_name}')
Exception: Sorry, the lower threshold=-1 should be larger than 0 for configuration=first


Traceback (most recent call last):
  File "", line 72, in <module>
    b1 = Bounds(1000, 125, 'first')
  File "", line 67, in __init__
    raise Exception(msg)
Exception: Upper bound must be greater than lower: Bounds(1000, 125, first)

Code with some possible edits for you to consider:

class Bounds:

    def __init__(self, lower, upper, name):
        self.lower = lower
        self.upper = upper = name

        if lower <= 0 or upper <= 0:
            msg = f'Bounds must be positive: {self}'
            raise ValueError(msg)

        if upper <= lower:
            msg = f'Upper bound must be greater than lower: {self}'
            raise ValueError(msg)

    def __repr__(self):
        return f'Bounds({self.lower}, {self.upper}, {!r})'

python – basic two-player tic tac toe game

Here is my code:

def printBoard(board):  # this prints the board
    print("         |         |         ")
    print(f"    {board(0)(0)}    |    {board(0)(1)}    |    {board(0)(2)}    ")
    print("         |         |         ")
    print("         |         |         ")
    print(f"    {board(1)(0)}    |    {board(1)(1)}    |    {board(1)(2)}    ")
    print("         |         |         ")
    print("         |         |         ")
    print(f"    {board(2)(0)}    |    {board(2)(1)}    |    {board(2)(2)}    ")
    print("         |         |         ")

def isGameOver(board, playerOneSprite, playerTwoSprite): #checks if eiither player won
    for i in range(3):
        if board(i)(0) == board(i)(1) and board(i)(1) == board(i)(2):
            if board(i)(0) == playerOneSprite:
                print("Player one wins!")
                return True
            elif board(i)(0) == playerTwoSprite:
                return True
        if board(0)(i) == board(1)(i) and board(1)(i) == board(1)(i):
            if board(0)(i) == playerOneSprite:
                print("Player one wins!")
                return True
            elif board(0)(i) == playerTwoSprite:
                return True
    if board(0)(0) == board(1)(1) and board(1)(1) == board(2)(2):
        if board(0)(0) == playerOneSprite:
            print("Player one wins!")
            return True
        elif board(0)(0) == playerTwoSprite:
            return True
    if board(0)(2) == board(1)(1) and board(1)(1) == board(2)(0):
        if board(0)(2) == playerOneSprite:
            print("Player one wins!")
            return True
        elif board(0)(2) == playerTwoSprite:
            return True
    return False

def switchTurns(playerOneSprite, playerTwoSprite, isPlayerOneTurn, board): #controls the flow of the game
    if isPlayerOneTurn:
        row = int(input("Player one, enter row: "))-1
        column = int(input("Player one, enter column: "))-1
        if (row > 3 or row < 1) or (column > 3 or column < 1):
            print("Out of range!")
            switchTurns(playerOneSprite, playerTwoSprite, isPlayerOneTurn, board)
        if board(row)(column) == '-':
            board(row)(column) = playerOneSprite
        row = int(input("Player two, enter row: "))-1
        column = int(input("Player two, enter column: "))-1
        if (row > 3 or row < 1) or (column > 3 or column < 1):
            print("Out of range!")
            switchTurns(playerOneSprite, playerTwoSprite, isPlayerOneTurn, board)
        if board(row)(column) == '-':
            board(row)(column) = playerTwoSprite
    if isGameOver(board, playerOneSprite, playerTwoSprite):
        switchTurns(playerOneSprite, playerTwoSprite, not isPlayerOneTurn, board)

switchTurns('X', 'O', True, board = (('-', '-', '-'), ('-', '-', '-'), ('-', '-', '-'))) #driver code

Is there a way to make this code smaller, more compact and readable?
Right no it is 72 lines long. I am hoping to get it below 60 lines, maybe even 50…
For instance, I have noticed that in the switchTurns() function some of the code is duplicated, but I am not sure how to implement that without sacrificing some clarity in the game…
Also, are there any improvements I can potentially make?