add download_url function & clean folders

This commit is contained in:
georgemihaila
2020-09-08 22:15:51 -05:00
parent 7bf21d59c5
commit 075f111616
8 changed files with 44 additions and 82 deletions

View File

@@ -1,20 +0,0 @@
# Python Debugging tool
To set debugging:
```python
import pdb; pdb.set_trace() #<---------DEBUGGING---------------
```
For example:
```python
def add(a, b):
'''
This is the test:
>>> add(2, 2)
5
'''
import pdb; pdb.set_trace() #<---------DEBUGGING---------------
return a + b
```

View File

@@ -1,36 +0,0 @@
# Download file from url inside of Python
## Imports
```python
import os
import requests
```
## Function:
```python
def download_from(url, path):
"""Download file from url.
Args:
url: Url to file.
path: Path to save file
Returns:
file_path: path where file is saved.
"""
file_name = os.path.basename(url)
os.makedirs(path) if not os.path.isdir(path) else None
file_path = os.path.join(path, file_name)
if not os.path.isfile(file_path):
response = requests.get(url)
file_size = open(file_path, 'wb').write(response.content)
file_size = '{0:.2f}MB'.format(file_size / 1024)
print("%s %s" % (file_path, file_size))
return file_path
```
## Use Function:
```python
download_from(url="https://raw.githubusercontent.com/gmihaila/machine_learning_things/master/sentiments/positive-words.txt", path="senwst")
```

View File

@@ -1,21 +0,0 @@
# File handling
Easy and simple read/write file handling.
## Imports
```python
import io
```
## Write to file
```python
io.open("my_file.txt", mode='w', encoding='utf-8').write("Hello!"))
```
## Read from file
```python
io.open("my_file.txt", mode='r', encoding='utf-8').read()
```

View File

@@ -1,5 +0,0 @@
# Universal Text Tool
Given any text as input, output any desired features for that text: tokens, sentences, embeddings, transformers embeddings.
Everything would be as inference model.

View File

@@ -7,6 +7,7 @@ __version__ = "0.0.1"
# functions imports
from .array_functions import (pad_array,
batch_array)
from .web_related import (download_from)
# alternative names
from .array_functions import batch_array as chunk_array

View File

@@ -0,0 +1,43 @@
# coding=utf-8
# Copyright 2020 George Mihaila.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Functions related to web applications"""
import os
import requests
def download_from(url, path):
"""Download file from url.
:param url: web path of file.
:param path: path to save the file.
:return: path where file was saved
"""
# get file name from url
file_name = os.path.basename(url)
# create directory frm path if it doesn't exist
os.makedirs(path) if not os.path.isdir(path) else None
# find path where file will be saved
file_path = os.path.join(path, file_name)
# check if file already exists
if not os.path.isfile(file_path):
# if files does not exist - download
response = requests.get(url)
file_size = open(file_path, 'wb').write(response.content)
file_size = '{0:.2f}MB'.format(file_size / 1024)
# print file details
print("%s %s" % (file_path, file_size))
return file_path