python – filecount in Rust

I have this python code and I am trying to recreate this in rust:

filecount = 0
while True:
    with open(f"file{filecount}.txt", "w") as file:
        file.write("Hello World")
        file.close()
        filecount += 1```


in Rust I try this but I get something about enum error when compiling.

see https://imgur.com/a/YgAK417
(Couldn't include my Rust code here as I get a message "Your post appears to contain code that is not properly formatted as code" and I'm not sure what's wrong with the formatting)

python – Naive Bayes Classifier for sentiment labelled documents

In order to continue improving my Python knowledge, I have implemented a naïve Bayes classifier as described in “An introduction to Information Retrieval”. I would be very interested which parts could be improved, be it e.g. coding style or use of data structures.

"""Implementation of a naive Bayes classifier based on sentiment labelled sentences.
See https://nlp.stanford.edu/IR-book/pdf/irbookonlinereading.pdf for the algorithm.
The dataset was obtained from
https://archive.ics.uci.edu/ml/datasets/Sentiment+Labelled+Sentences"""
import sys
import re
import math
from collections import Counter
from stop_words import get_stop_words


# PARAMETERS
DATAFILE = "data\imdb_labelled.txt"

# FUNCTIONS
def load_data(filepath):
    """Load the sentiment labelled data."""
    # A library is a list of categories, which label a list of documents
    library = ((),())

    # The textfile is formatted as document (string), TAB, category (int), NL
    with open(filepath, 'r') as file:
        for line in file:
            document, category = line.split('t')
            library(int(category)).append(document)

    return library

def clean_library(library):
    """Clean documents in the library array."""
    for i, category in enumerate(library):
        for j, document in enumerate(category):
            cleaned_doc = clean_document(document)
            library(i)(j) = cleaned_doc

def clean_document(document):
    """Clean a document from stop words, numbers and various other
       characters and return a list of all words."""
    stop_words = get_stop_words('en')

    new_doc = document.strip().lower()
    new_doc = re.sub("(-0-9.,!;:\/()"&)", "", new_doc)
    new_doc = new_doc.split()
    new_doc = (word for word in new_doc if word not in stop_words)

    return new_doc

def train_categories(library):
    """Calculate probabilities for the naive Bayes classifier and
       return the vocabulary with conditional probabilities and the priors."""
    total_docs = sum((len(category) for category in library))
    vocabulary = (word for category in library
                       for document in category
                       for word in document)

    cond_prob = ()
    prior = ()

    for category in library:
        # Prior probability
        total_cat_docs = len(category)
        prior.append(total_cat_docs / total_docs)

        # Conditional probabilities
        text = (word for document in category for word in document)

        word_count = Counter(text)
        total_word_count = sum(word_count.values())

        cat_cond_prob = {}

        for word in vocabulary:
            cat_cond_prob(word) = (word_count(word) + 1) / (total_word_count + 1)

        cond_prob.append(cat_cond_prob)

    return (vocabulary, prior, cond_prob)

def apply_nb(vocabulary, priors, cond_prob, document):
    """Apply the naive Bayes classification to a document in order
       to retrieve its category."""
    prepared_doc = clean_document(document)
    prepared_doc = (word for word in prepared_doc if word in vocabulary)

    score = (math.log(prior) for prior in priors)

    for cat, cat_cond_prob in enumerate(cond_prob):
        score(cat) = sum((math.log(cat_cond_prob(word)) for word in prepared_doc))

    return score.index(max(score))

def main(argv):
    """Train a naive Bayes classifier and apply it to a user-supplied string."""
    if len(argv) == 0:
        print("Please supply a document string.")
        return

    user_doc = argv(0)

    library = load_data(DATAFILE)
    clean_library(library)
    vocabulary, priors, cond_prob = train_categories(library)

    doc_cat = apply_nb(vocabulary, priors, cond_prob, user_doc)
    print(f'"{user_doc}": Category {doc_cat}')

if __name__ == "__main__":
    main(sys.argv(1:))

python – Pyautogui screenshot – .save not going to given path

I’m using Pyautogui to take a screenshot of a portion of the screen, below is a snippet of the code – the code works fine but the created file just seems to be created in the same directory as the python file. When given a path name it takes part of it and makes it into its name.

The code:

    def takeBoundedScreenShot(self, x1, y1, x2, y2):
        im = pyautogui.screenshot(region=(x1, y1, x2, y2))
        x = datetime.datetime.now()
        fileName = x.strftime("%f")
        im.save(r'C:UsersuserPycharmProjectsPDFCaptureoutput_folder_one' + fileName + ".png")

with this file path the created file is stored in the same folder as the python project but with the file name ‘output_folder_one’ + ‘fileName’

I have a folder in this directory called output_folder_one – I am not sure why the created files are not being stored in there.

Any insight would be greatly appreciated!! :

python – Booking an East London Tennis Court

I’m not sure if it Covid-19 but lately it is impossible to book a tennis court in my area on time. It’s always full or maybe just don’t check enough 🙂

To beat the queue and get notified earlier, I started making a scraper to ensure I can book a court on time.

But because of iframe hell and the lack (to my knowledge) of an API it was impossible to do it the easy way, hence I used selenium. I coded this today and it works pretty well, although it’s already become a bit of a mess.

Dynamically detecting the hours available and starting time of the day, so I don’t have to supply those values anymore, because my guess is they will change (depending on the time it will get dark etc.)

You can still flame me for it, but I just haven’t found a good way to do it yet, now I just supply them in the COURTS dictionary

#!/usr/bin/python3

import time

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

# At what minimal time I am willing/able to book
WEEKEND_TIME = 10
WEEK_TIME = 18

COURTS = {
    # Some courts have different hours available and the hour at which you can start can differ
    # Thus it is key -> name with values -> url, hours available, time diff start
    "Bethnal Green Gardens" : ("https://www.towerhamletstennis.org.uk/bethnal-green-gardens-court-bo", 14, 0),
    "St Johns Park"         : ("https://www.towerhamletstennis.org.uk/st-johns-park-court-boo", 14, 1),
    "Poplar Rec"            : ("https://www.towerhamletstennis.org.uk/poplar-rec-court-bookin", 12, 0),
    "King Edward's Park"    : ("https://www.towerhamletstennis.org.uk/kemp-court-bookin", 12, 0),
    "Victoria Park"         : ("https://www.towerhamletstennis.org.uk/victoria-park-court-book", 12, 0),
}

def get_week_schedule(driver, time_length, time_start, first_week=True):
    elem = driver.find_element_by_xpath('//div(@class="cal_time cal_time_time")')
    sched = (e.text for e in elem.find_elements_by_xpath('//div(@class="inc")'))
    times = sched(time_start:17)
    active = sched(17:)

    # When first week we only want from x till sunday and second week monday till x
    days = ("Sunday", "Saturday", "Friday", "Thursday", "Wednesday", "Tuesday", "Monday")
    active_days = days(:len(active) // time_length)(::-1) if first_week 
             else days(::-1)(:len(active) // time_length)
    
    week_schedule = {}
    for i, active_day in enumerate(active_days):
        week_schedule(active_day) = {
            int(times(j).split(":")(0)) : active(i * time_length + j)
            for j in range(time_length)
        }
    return week_schedule

def get_court_schedule(driver, data):
    url, time_length, time_start = data
    driver.get(url)
    
    # Yo dawg, I heard you like iframes so I put an iframe in your iframe...
    WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.XPATH, "//iframe")))    
    driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
    driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))

    court_schedule = {}
    court_schedule("this") = get_week_schedule(driver, time_length, time_start)

    # Next week is second element
    elem = driver.find_elements_by_xpath("//a(@href)/span")
    elem(1).click()
    # TODO Fix time.sleep...  but same page so how can we wait?
    time.sleep(5)
    court_schedule("next") = get_week_schedule(driver, time_length, time_start, first_week=False)

    return court_schedule

def get_schedule():
    driver = webdriver.Chrome()

    court_schedules = {
        court_name : get_court_schedule(driver, url)
        for court_name, url in COURTS.items()
    }

    driver.quit()
    return court_schedules

def get_available_dates(schedule):
    for court_name, court_schedule in schedule.items():
        for week, week_schedule in court_schedule.items():
            for day, day_schedule in week_schedule.items():
                earliest_time = WEEKEND_TIME if day in ("Saturday", "Sunday") else WEEK_TIME
                for hour, available in day_schedule.items():
                    if hour >= earliest_time and available not in ("Full", " "):
                        print(f"Can book at court {court_name} {week} week on {day} at {hour} for price {available}")

if __name__ == "__main__":
    schedule = get_schedule()
    get_available_dates(schedule)
./tennis.py
Can book at court Poplar Rec next week on Saturday at 10 for price £6
Can book at court Poplar Rec next week on Saturday at 11 for price £6
Can book at court King Edward's Park this week on Saturday at 18 for price £6
Can book at court King Edward's Park next week on Friday at 18 for price £6
Can book at court King Edward's Park next week on Saturday at 10 for price £6
Can book at court King Edward's Park next week on Saturday at 11 for price £6

python – Running Deepfake for Baka Mitai Meme

I’m a very new coder and was trying to run the baka mitai meme code as a joke but I have run into a problem.

from demo import load_checkpoints
generator, kp_detector = load_checkpoints(config_path=’config/vox-256.yaml’,
checkpoint_path=’/content/gdrive/My Drive/first-order-motion-model/vox-cpk.pth.tar’)

ModuleNotFoundError Traceback (most recent call last)
in ()
—-> 1 from demo import load_checkpoints
2 generator, kp_detector = load_checkpoints(config_path=’config/vox-256.yaml’,
3 checkpoint_path=’/content/gdrive/My Drive/first-order-motion-model/vox-cpk.pth.tar’)

ModuleNotFoundError: No module named ‘demo’

As you can see, something isn’t working. Anyone know how to fix this. All online tutorials I’ve seen never address or run into this.

python – Optimizing code in codewars

I was practicing python on codewars.com with this question:

Create a function that takes a positive integer and returns the next bigger number that can be formed by rearranging its digits. For example:

12 ==> 21

513 ==> 531

2017 ==> 2071

But when I attempted my solution it said it was not optimized enough

Can someone say why it said that?

from itertools import permutations
def next_bigger(n):
    per = list(permutations(str(n)))
    result = ()
    for j in per:
        result.append(int("".join(j)))
    for i in sorted(result):
        if i > n:
            return i

algorithms – Logarithmic Time — O(log n) in Python

Hi , I am new to Data Structure and trying to get some clarifications.

Following shows an example Logarithmic Time — O(log n) in Python.

def binary_search(data, value):
    n = len(data)
    left = 0
    right = n - 1
    while left <= right:
        middle = (left + right) // 2 # How can make this by 3 to make the search faster
        if value < data(middle):
            right = middle - 1
        elif value > data(middle):
            left = middle + 1
        else:
            return middle
    raise ValueError('Value is not in the list')
    
if __name__ == '__main__':
    data = (1, 2, 3, 4, 5, 6, 7, 8, 9)
    print(binary_search(data, 8))

Can you please explain and write the code on how can make this faster? Maybe for n/3

Thanks

Should I stub hidden dependencies in Python unit tests

Python is very specific language which gives developer huge flexibility.

So if I have class like this:

class Car:
    def __init__(self, engine: Engine):
        self._engine = engine  # dependency injection
        self.speed = 0

    def accelerate(self, target_speed: int):
        if self._engine.can_accelerate_to_speed(target_speed):
            self.speed = target_speed

testing it is a piece of cake.

def test_car_acceleration():
    car = Car(engine=MagicMock())
    car.accelerate(100)
    assert car.speed == 100

But what if the engine dependency is hidden?

class SlowCar:
    def __init__(self):
        self.engine = DefaultEngine()  # hidden dependency or maybe implementation detail?
        self.speed = 0

    def accelerate(self, target_speed: int):
        if self.engine.can_accelerate_to_speed(target_speed):
            self.speed = target_speed

What is the right approach for the SlowCar? Let’s assume SlowCar is intended to have DefaultEngine mounted by default, but nothing prevents class consumer from doing so:

car = SlowCar()
car.engine = HEMI()

Anyway should I treat SlowCar.engine as dependency or implementation detail?
Should I patch it out?

def test_slow_car():
    with patch('path.to.SlowCar.DefaultEngine'):
        car = SlowCar()
        car.accelerate(100)
    assert car.speed == 100

def test_slow_car_way2():
    car = SlowCar()
    car.engine = MagicMock()
    car.accelerate(100)
    assert car.speed == 100

I guess that while SlowCar.engine is “public” field I may consider patching it as it’s part of public API so it seems to be clearly a dependency.

But if I would have SlowCar._engine.. A “private” field is not part of public API so it seems more like implementation detail and it should not be mocked/patched. That’s internal part of SlowCar which I as a consumer should not care about, right? It’s tightly coupled to SlowCar and SlowCar does work with it or does not work at all.