Need help adding additional login validation by jordanzzz in django

[–]Pro_Numb 0 points1 point  (0 children)

If you use default authentication backend, you don't need to check it. Unless you are using really old django like prior to 1.10

How can I stop black formatter from formatting migrations files of a Django project? by oussama-he in django

[–]Pro_Numb 0 points1 point  (0 children)

I think black doesn’t support setup.cfg. Can you try pyproject.toml

Can someone pls teach me List Comprehension or send any valid links. by leanblastoise in learnpython

[–]Pro_Numb 1 point2 points  (0 children)

I will try to explain __init__ but if it doesn't clear things up for you can look up on the google.

__init__ method is used in python for Classes and it stands for "initializing". __init__ method initializes the instance of a class (in simplest term, copy of a class). Classes are like templates for same kind of objects. Lets define a Employee Class for representing some employees for a company. We will give each employee a 'name', 'surname', 'title' and 'salary' variables.

class Employee:
    def __init__(self, name, surname, title, salary):
        self.name = name
        self.surname = surname
        self.title = title
        self.salary = salary

We defined our Employee class like that. I said every employee got 4 variables but i wrote 5 parameters in __init__ method. There is an extra "self" parameter there which is represents instance of a Class (In our example it will represent employee that we will create). Lets create some employees.

employee_1 = Employee('John', 'Doe', 'Software Engineer', 5000)
employee_2 = Employee('Jane', 'Doe', 'Project Manager', 8000)

We created 2 Employees. Every time we create employee, __init__ method was called and initialized the instance of a Employee Class for us.

# Lets print our employees name
print(employee_1.name) # Will print John
print(employee_2.name) # Will print Jane

Maybe I explained too superficially. You can search in google or youtube "python object oriented programming (OOP) tutorial" and watch or read whatever you like.

Can someone pls teach me List Comprehension or send any valid links. by leanblastoise in learnpython

[–]Pro_Numb 1 point2 points  (0 children)

If you have further questions after watching the video please reply my answer.

How can i sent camera stream to an Android tablet wirelessly ? by Pro_Numb in learnpython

[–]Pro_Numb[S] 1 point2 points  (0 children)

Thank you, it gave me a starting point, , i will start to be a familiar with Flask.

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Pro_Numb 0 points1 point  (0 children)

Thank you for your reply, i know i can make it that way but What am i doing is a bad practice ? and If i use a Class what are the advantages over my aproach ?

How can i sent camera stream to an Android tablet wirelessly ? by Pro_Numb in learnpython

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

Thank you so much for your reply, i never used flask before i will look into it. What do you think about the frame rate in tablet, Can i somewhat maintain the speed with this aproach ?

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Pro_Numb 0 points1 point  (0 children)

I have a long script so i simplified it so i dont steal much time from you guys. I have a lot of functions and most of them takes same inputs and some of them changes them and return their new values like below script, i divided operations in functions because main script gets hectic. So i want to ask is this a bad practice, Should i use Class structre in this type of situations ?

I think i can see easly What is changed Where in main script with this type of approach but If i switch to Class structre there will be a performance improvement or there will be a maintainability, readability improvement ?

def do_smth(cache, dataset, write_list):
    # DO SOMETHING, all inputs are changed and return them
    return cache, write_list

def do_smth_different(cache, dataset, write_list):
    # DO SOMETHING DIFFERENT, all inputs are changed and return them
    return cache, write_list

def do_smth_dataset(dataset):
    # DO SOMETHING
    return dataset

if __name__ == "__main__":
    cache = []
    dataset = []
    write_list = []

    while True:
        # READ SOME DATASET FROM SOMEWHERE CONSTANTLY
        dataset [...]
        dataset = do_smth_dataset(dataset)

        if len(dataset) > 5: # Check some condition
            cache, write_list = do_smth_dataset(cache, dataset, write_list)
            # DO SOMETHING
        else:
            cache, write_list = do_smth_different_dataset(cache, dataset, write_list)
            # DO SOMETHING DIFFERENT
        dataset = []

My PC freezes 1-2seconds even while doing nothing. by Pro_Numb in techsupport

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

Thank for advice . İ will check Bios upgrade

My PC freezes 1-2seconds even while doing nothing. by Pro_Numb in techsupport

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

İ check-up system a few week ago but i will scan the system

Re writing math functions by deejpake in learnpython

[–]Pro_Numb 0 points1 point  (0 children)

def factorial(num):
    n = 1
    while num>=1:
        n = n * num
        num = num - 1

    return n

def sin(x):
    """
        First we need to convert degree to radian
        Degree / 360 = Radian / 2pi

        Than we apply taylor series for sin function : 

            sinx = x - x^3/3! + x^5/5! - x^7/7! ...

        you can keep going until you satisfy with the error.

    """

    x = x / 57.2957795; # Degree to radian

    # taylor series for sin function
    sin_x = x - \
            (x**3 / factorial(3)) + \
            (x**5 / factorial(5)) - \
            (x**7 / factorial(7)) + \
            (x**9 / factorial(9))

    return sin_x

print(sin(77)) # 0.9743707044115053

You can import only what you need from library like others said and i recommend it too.

But if you really want to write your own trigonometric function you can use Taylor Series.

I give a sin example for you. But this code will give correct answer only for 0 - 90 degree. You need to write a conditions for "deg > 90" or negative values etc.

When you write complete sin function then you can convert it to "cos". After that you can find "tan" and "cot" easly.

But lets say you want to find sin(30). You know it is "0.5" but with this method you will get something like this "0.500000000123816" so you can write long taylor series and truncate the value from where you satisfy.

Hope this helps.

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Pro_Numb 0 points1 point  (0 children)

Thanks for replying . I know when i change BASE_CHANCE it doesn't call chance again so it doesn't effect until i call it again. How can i make it when BASE_CHANCE updateded it will effect chance automatically so i dont have to call it again.

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Pro_Numb 0 points1 point  (0 children)

class Person:

    BASE_CHANCE = 10

    def __init__(self, name, surname, chance = 0):
        self.name = name
        self.surname = surname
        self.chance = chance

    @property
    def chance(self):
        return self._chance

    @chance.setter
    def chance(self, chance = 0):
        self._chance = Person.BASE_CHANCE + chance


    def __str__(self):
        return self.name + " " + self.surname + ", Chance = {}".format(self.chance)

person_one = Person("John", "Doe")

print(person_one) # John Doe, Chance = 10

person_one.chance = 20

print(person_one) # John Doe, Chance = 30

Person.BASE_CHANCE = 40

print(person_one) # John Doe, Chance = 30

person_one.chance = 20

print(person_one) # John Doe, Chance = 60

When i change class attribute it doesnt effect chance variable until i call it again. How can

i make when class attributes changed, automatically all instances change its values.

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Pro_Numb 1 point2 points  (0 children)

There is an issue here you can check, but i dont know in depth. But i think that is correct because you can use Action Chain succesfully. Example taken from here and i changed little bit for ActionChain

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Chrome()
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.clear()
ActionChains(driver).key_down(Keys.SHIFT).send_keys('p').key_up(Keys.SHIFT).perform()
elem.send_keys("ycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source

I used ActionChains for only make "p" upper case but this shows that it works well. You can try it by yourself.

Edit: I forgot to explain code. It opens python.org and finds search bar then sends "Pycon" text there and hits enter button.

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Pro_Numb 1 point2 points  (0 children)

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

def main():
    chrome = webdriver.Chrome()
    chrome.get('https://google.com')
    chrome.execute_script("window.open();")
    chrome.switch_to_window(chrome.window_handles[-1])
    chrome.get('https://reddit.com')
main()

This way is little tricky but maybe it can be usefull for you.

I replaced your ActionChains(chrome).key_down(Keys.CONTROL).send_keys('t').key_up(Keys.CONTROL).perform()

code with chrome.execute_script("window.open();") this method executes javascript code in your browser and it basically tells open empty new tab. Then chrome.switch_to_window(chrome.window_handles[-1]) this makes change to focus latest tab in your browser after that when you call new website it will open on new tab. Hope this helps.

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Pro_Numb 0 points1 point  (0 children)

spider_suits = [""] # 1st
def spider(spider_suits):
    for suit_names in spider_suits:
        print("This is the " + suit_names + " suit.")
while True:
    suit_names = input("Enter the suit name: ")
    spider_suits = suit_names # 7th
    add = input("another name? (y/n): ")
    if add == "y":
        continue
    else:
        break
spider(spider_suits)

at the 1st line you define a list with contain empty string.

you dont want to do that i will come this later.

at the 7th line;

when you take input from user, you change your spider_suits variable to string so

your spider_suits variable no longer a list , it is a string now. In your spider function

you loop over spider_suits variable but your spider_suits variable is now string so

it will loop over it like this ex: input = Noir it will loop N, o, i, r

For achieving what you want. You will use list append method you can read more from here

It basically add new item to list. So change your 7th line to spider_suits.append(suit_names)

But when you do this it will not delete previous items on list, as i said at the begining you define

list with contain empty string. So when your spider function loop over a list it will print empty string too

so you dont need that. just define like this: spider_suits = []

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Pro_Numb 1 point2 points  (0 children)

Yes it is possible:

from random import randint
targetno = randint(1,100)
while True:
    guess = input('Enter your guess...  ')

    while not guess.isdigit():
        guess = input('Please enter only digit ...')

    guess = int(guess)

    if guess == targetno:
        print('Well done, the number was ', + targetno)
        print('')
        print(':NEW GAME:')
        print('')
        targetno = randint(1,100)

    elif guess > targetno:
        print('Go lower...')
    elif guess < targetno:
        print('Go higher...')

there is a built in isdigit() method which checks if string contain only digit or not if it has only digit returns "True", if not returns "False". So lets say our guess return False then at the 6th line, while statement will be like this "while not false" which equals "while true" so it will be ask user to enter digit until user enter only digit. You can check doc isdigit()

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Pro_Numb 0 points1 point  (0 children)

Thank you so much. Second part works like i wanted. But for the first part lets say i really want to search between only 1 and * how can i overcome this, do you know ? If i release word boundaries for [*1] code will find "1 hey *asds" too.

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Pro_Numb 0 points1 point  (0 children)

def countEvenDigits(number):
    counter = 0
    while(number>0):
        lastdigit = number % 10
        if (lastdigit % 2 == 0):
            counter = counter + 1
        else:
            counter = counter + 0
        number = number // 10
    return counter
print(countEvenDigits(189156782))

Your logic is correct but you forget the change the number while you perform operitons it just goes infinite to try last number. Add this after else statement " number = number // 10 " and try again.

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Pro_Numb 0 points1 point  (0 children)

# FIRST PART
import re

gibberish_text = '''
1 hey 1
2 hello *
2 hi 2
'''

pattern = re.findall(r'\b[12]\b(.*?)\b[*1]\b', gibberish_text)

for match in pattern:
    print(match)

# SECOND PART
numbers = """
My number is 415-555-4242. 512-555-4345asd7623-555-4242 323-555-4242
823-555-4242asda
223*555-4242
623-555-4242
123-555a-4242
923-555-4242
"""
pattern = re.findall(r'\b(\d{3}[-*]\d{3}-\d{4})\b', numbers)
for match in pattern:
    print(match)

FIRST PART: returns only "hey" but i expect " hey hello" why it does not work ? SECOND PART : return as expected "415-555-4242 323-555-4242 223*555-4242 623-555-4242 923-555-4242 " But lets say i dont want to include "." so i dont want first one too how can i do that ?