Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

random.codon:263:13-16: error: name 'INF' is not defined #497

Open
tuneman7 opened this issue Nov 17, 2023 · 1 comment
Open

random.codon:263:13-16: error: name 'INF' is not defined #497

tuneman7 opened this issue Nov 17, 2023 · 1 comment

Comments

@tuneman7
Copy link

This seems like a great project, I'm unsure why I'm getting this error.

environment Ubutu Docker image.

Here is my Command

codon run gen_random_data.py

--- Here is the error I receive

gen_random_data.py:1:8-11: error: no module named 'csv'
random.codon:263:13-16: error: name 'INF' is not defined
random.codon:661:24-31: error: name '_bisect' is not defined

Below is the code:

import csv
import os
import random
import string
from datetime import datetime, timedelta

def random_string(length=10):
"""Generate a random string of fixed length."""
return ''.join(random.choice(string.ascii_lowercase) for _ in range(length))

def random_date(start, end):
"""Generate a random datetime between start and end."""
return start + timedelta(
seconds=random.randint(0, int((end - start).total_seconds())),
)

def generate_csv(filename, size_limit=1024**3):
"""Generate a CSV file with a target size limit."""
with open(filename, 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['id', 'name', 'timestamp', 'value'])

    total_size = 0
    id_counter = 1
    start_date = datetime(2020, 1, 1)
    end_date = datetime(2023, 1, 1)

    while total_size < size_limit:
        row = [
            id_counter,
            random_string(10),
            random_date(start_date, end_date).strftime('%Y-%m-%d %H:%M:%S'),
            random.uniform(0, 1000)
        ]
        writer.writerow(row)
        id_counter += 1
        total_size = file.tell()

generate_csv('sample_data_to_transform.csv')

@arshajii
Copy link
Contributor

Hi @tuneman7 -- Codon doesn't support the CSV module natively yet, but you can replace that with writing to the file directly:

import os
import random
import string
from datetime import datetime, timedelta

def random_string(length=10):
    """Generate a random string of fixed length."""
    return ''.join(random.choice(string.ascii_lowercase) for _ in range(length))

def random_date(start, end):
    """Generate a random datetime between start and end."""
    return start + timedelta(
        seconds=random.randint(0, int((end - start).total_seconds())),
    )

def generate_csv(filename, size_limit=1024**3):
    """Generate a CSV file with a target size limit."""

    def write_row(f, row):
        for i in range(len(row)):
            f.write(row[i])
            if i < len(row) - 1:
                f.write(', ')
        f.write('\n')

    with open(filename, 'w') as file:
        write_row(file, ('id', 'name', 'timestamp', 'value'))
        total_size = 0
        id_counter = 1
        start_date = datetime(2020, 1, 1)
        end_date = datetime(2023, 1, 1)

        while total_size < size_limit:
            row = (
                str(id_counter),
                random_string(10),
                str(random_date(start_date, end_date)),  # don't support strftime yet, but str() gives same format
                str(random.uniform(0, 1000))
            )
            write_row(file, row)
            id_counter += 1
            total_size = file.tell()

generate_csv('sample_data_to_transform.csv', size_limit=1000)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants