How to create a csv file from a folder? - python

So my folder structure looks like this
MP_Data
|___dir1
| | 0
| |___ 0.npy
| |___ 1.npy
| .
| .
| 29
| | 1
| |___ 0.npy
| |___ 1.npy
| .
| .
| 29
| .
| .
| 29
|___dir2
| | 0
| |___ 0.npy
| |___ 1.npy
| .
| .
| 29
| | 1
| |___ 0.npy
| |___ 1.npy
| .
| .
| 29
| .
| .
| 29
|___dir3
| | 0
| |___ 0.npy
| |___ 1.npy
| .
| .
| 29
| | 1
| |___ 0.npy
| |___ 1.npy
| .
| .
| 29
| .
| .
| 29
So I need csv file for including dataset for creating model for deploying it using flask. The csv file should contain all the data present inside the MP_Data folder. I am super new to flask so I've no clue how to do this, can anyone pls help me?

Quite simple.
Use the following approach (relative to where you've saved your python file).
I'll be assuming you've saved your python file in MP_Data and that your CSV file is in dir1.
import pandas as pd
df = pd.read_csv('dir1/data.csv')
print(df)

I am using this approach!
import pandas as pd
#loading the data
data = pd.read_csv('file:///C:/Users/d/Downloads/Nifty_50_Futures_Historical_Data.csv')
print(data)

Related

Pytest doesn't seem to recognize and run my tests

So the previous answer to this question doesn't seem to work for me. But I think this problem has something to do with my package references or my setup.cfg file.
I have two tests (just to start with to try and get this working) the results are:
================================ test session starts ==================================================================================================================================================================
platform win32 -- Python 3.7.10, pytest-6.1.1, py-1.9.0, pluggy-0.13.1 -- C:\Users\heasm\Anaconda3\envs\UofS\python.exe
cachedir: .pytest_cache
rootdir: C:\USask Python\geo_calcs, configfile: setup.cfg
collected 0 items
================================= warnings summary ====================================================================================================================================================================
..\..\Users\heasm\Anaconda3\envs\UofS\lib\site-packages\_pytest\config\__init__.py:1230
C:\Users\heasm\Anaconda3\envs\UofS\lib\site-packages\_pytest\config\__init__.py:1230: PytestConfigWarning: Unknown config option: collect_ignore
self._warn_or_fail_if_strict("Unknown config option: {}\n".format(key))
-- Docs: https://docs.pytest.org/en/stable/warnings.html
================================ 1 warning in 0.61s ===================================================================================================================================================================
Now my setup.cfg file is:
current_version = 0.1.0
commit = True
tag = True
[bumpversion:file:setup.py]
search = version='{current_version}'
replace = version='{new_version}'
[bumpversion:file:geo_calcs/__init__.py]
search = __version__ = '{current_version}'
replace = __version__ = '{new_version}'
[bdist_wheel]
universal = 1
[flake8]
exclude = docs
[tool:pytest]
collect_ignore = ['setup.py']
My test cases all seem to be named correctly. Here is an example:
from geo_calcs.calculations.geochem import *
#pytest.fixture
def test_get_atomic_weight():
assert get_atomic_weight(["Si","O","O"]) == 60.0843
Finally my directory structure is:
+---geo_calcs
| | .editorconfig
| | .gitignore
| | .travis.yml
| | AUTHORS.rst
| | CONTRIBUTING.rst
| | HISTORY.rst
| | LICENSE
| | Makefile
| | MANIFEST.in
| | README.rst
| | requirements_dev.txt
| | setup.cfg
| | setup.py
| | tox.ini
| +---docs
| | authors.rst
| | conf.py
| | contributing.rst
| | history.rst
| | index.rst
| | installation.rst
| | make.bat
| | Makefile
| | readme.rst
| | usage.rst
| |
| +---geo_calcs
| | | utils.py
| | | __init__.py
| | |
| | +---calculations
| | | | geochem.py
| | | | geochron.py
| | | | __init__.py
| | | |
| | +---visualizations
| | | __init__.py
| |
| \---tests
| | test_utils.py
| | __init__.py
| |
| +---calculations
| | | test_geochem.py
| | | test_geochron.py
| | | __init__.py
| |
| +---visualizations
| | | __init__.py
Does anyone have any insight?
Remove the #pytest.fixture decorator from above the test. Tests shouldn't have that, they're discovered by naming convention.
Pytest uses glob-style name patterns to collect tests, and the discovery can be customized by the options python_files, python_classes, and python_functions in the configuration file.
The default patterns are:
[pytest]
python_files=test_*.py
python_classes=Test*
python_functions=test_*

Multiple static folders & templates folders in flask app

Is it possible to have more than one static folder in flask app? I have some .css and .js files which I am using only for certain blueprint and would like to have the files in same directory as blueprint.
My app has structure like this:
app
|
| blueprints
| |
| | blueprint_1
| | |
| | | views.py
| | | __init__.py
| |
| | blueprint_2
| | |
| | | views.py
| | | __init__.py
|
| static
| |
| | css, js, etc.
And I would like to have structure like this:
app
|
| blueprints
| |
| | blueprint_1
| | |
| | | views.py
| | | __init__.py
| | | static
| | | |
| | | | certain css, js, etc
| |
| | blueprint_2
| | |
| | | views.py
| | | __init__.py
| | | static
| | | |
| | | | certain css, js, etc
|
| static
| |
| | css, js, etc.
And similar thing with templates.
While I am using .js file in template I access it with:
{{url_for('static'), filename='jsfile'}}
Okay, I figured it out.
According to docs, if the blueprint does not have a url_prefix, it is not possible to access the blueprint’s static folder.
The solution then is:
Add url_prefix to Blueprint:
some_blueprint = Blueprint('something', __name__, static_folder='static', url_prefix='/something')
and in the template we refer to our static folder as it follows:
{{url_for('something.static', filename=...)}}
It seems kinda weird to me, that url_prefix is necessary for it to work, but whatever.

how to import a function from parent directory into test directory

I am trying to import a function from the directory in the same project. how can i do that ?
.|____project/
| |_____src/
| | |____myproject/
| | | | __init__.py
| | | |____funtion.py
| |_____test/
| | |___ testmyproject.py
| | |____data/
| | |___testdata
How can i import the the file function.py or the functions in the function.py file in my test/testmyproject.py to test the fucntions? i am not able to import the module from function.py

Pythonic way to print a chess board in console

I was wondering what would be the best pythonic way to print a console chess board using a dictionary.
Should I use list ? but what happens if their is no piece on the board on a certain row?
Edit :
In chess programs (typically for computing best moves, solve riddles, etc.) internally a 10×10 board is used where the middle 8×8 fields are used by the visible and playable board. (The margin helps keep the code simple.) To simplify memory management, typically a linear memory is used to store this, i. e. 100 fields. The position of a field of the board is then computed using board[(y+1) * 10 + (x+1)] with x, y in the range 0..7. (Init by board = [ 0 ] * 100.)
So you might want to use this, just in case your program later should expand to also compute moves or stay compatible with a library doing this.
As for graphics, I propose to use this board:
A B C D E F G H
------- ------- ------- ------- ------- ------- ------- -------
| #___# | %~b | .#. | \o*o/ | __+__ | .#. | %~b | #___# |
8| ### | `'dX | ### | ### | `###' | ### | `'dX | ### |8
| d###b | d##b | ./A\. | d###b | d###b | ./A\. | d##b | d###b |
------- ------- ------- ------- ------- ------- ------- -------
| _ | _ | _ | _ | _ | _ | _ | _ |
7| (#) | (#) | (#) | (#) | (#) | (#) | (#) | (#) |7
| d#b | d#b | d#b | d#b | d#b | d#b | d#b | d#b |
------- ------- ------- ------- ------- ------- ------- -------
| | . . . | | . . . | | . . . | | . . . |
6| | . . . | | . . . | | . . . | | . . . |6
| | . . . | | . . . | | . . . | | . . . |
------- ------- ------- ------- ------- ------- ------- -------
| . . . | | . . . | | . . . | | . . . | |
5| . . . | | . . . | | . . . | | . . . | |5
| . . . | | . . . | | . . . | | . . . | |
------- ------- ------- ------- ------- ------- ------- -------
| | . . . | | . . . | | . . . | | . . . |
4| | . . . | | . . . | | . . . | | . . . |4
| | . . . | | . . . | | . . . | | . . . |
------- ------- ------- ------- ------- ------- ------- -------
| . . . | | . . . | | . . . | | . . . | |
3| . . . | | . . . | | . . . | | . . . | |3
| . . . | | . . . | | . . . | | . . . | |
------- ------- ------- ------- ------- ------- ------- -------
| _ | _ | _ | _ | _ | _ | _ | _ |
2| ( ) | ( ) | ( ) | ( ) | ( ) | ( ) | ( ) | ( ) |2
| /_\ | /_\ | /_\ | /_\ | /_\ | /_\ | /_\ | /_\ |
------- ------- ------- ------- ------- ------- ------- -------
| [___] | %~\ | .O. | \o^o/ | __+__ | .O. | %~\ | [___] |
1| [ ] | `')( | \ / | [ ] | `. .' | \ / | `')( | [ ] |1
| /___\ | <__> | /_\ | /___\ | /___\ | /_\ | <__> | /___\ |
------- ------- ------- ------- ------- ------- ------- -------
A B C D E F G H
I made these ASCII graphics back in the 90s for Tubmud. Feel free to use them.
I would use a nested list for that problem. In chess, you have an 8x8 grid, which can easily built by python list comprehension magic.
grid = [ [" "] * 8 for unused in range(8)]
You than can access and alter the elements by indexing an array:
grid[0][0] = 'T'
You can use lower case letters for white and upper case letters for black figures, and their first letter or so for the kind, like 'Q' for black queen.
Dictionary looks not good for me, since it is hard to make operations for checking valid moves or sth, because you have no other way to determine the position of a figure by iterating over all values. The grid is most natural.

Python - How to PYTHONPATH with a complex directory structure?

Consider the following file\directory structure:
project\
| django_project\
| | __init__.py
| | django_app1\
| | | __init__.py
| | | utils\
| | | | __init__.py
| | | | bar1.py
| | | | ...
| | | ...
| | django_app2\
| | | __init__.py
| | | bar2.py
| | | ...
| | ...
| scripts\
| | __init__.py
| | foo.py
| | ...
How should I use sys.path.append in foo.py so that I could use bar1.py and bar2.py?
How would the import look like?
Using relative paths would be much more desirable for portability reasons.
At the top of your foo.py script add the following:
import os, sys
PROJECT_ROOT = os.path.join(os.path.realpath(os.path.dirname(__file__)), os.pardir)
sys.path.append(PROJECT_ROOT)
# Now you can import from the django_project package
from django_project.django_app1.utils import bar1
from django_project.django_app2 import bar2
import sys
sys.path.append('/absolute/whatever/project/django_project/django_app1')
sys.path.append('/absolute/whatever/project/django_project/django_app2')
Though you need to evaluate whether you want to have both in your path -- in case there are competing module names in both. It might make sense to only have up to django_project in your path, and call django_app1/bar1.py when you need it and import django_app2.bar2.whatever when you need it.

Categories

Resources