What I am trying to do is, save each recorded file with a different filename(Myaudio1,2,3). Currently this code saves the audio as MYAUDIO.3gp, replacing the old file.
class MyRecorder:
def __init__(self):
'''Recorder object To access Android Hardware'''
self.MediaRecorder = autoclass('android.media.MediaRecorder')
self.AudioSource = autoclass('android.media.MediaRecorder$AudioSource')
self.OutputFormat = autoclass('android.media.MediaRecorder$OutputFormat')
self.AudioEncoder = autoclass('android.media.MediaRecorder$AudioEncoder')
# create out recorder
self.mRecorder = self.MediaRecorder()
self.mRecorder.setAudioSource(self.AudioSource.MIC)
self.mRecorder.setOutputFormat(self.OutputFormat.THREE_GPP)
self.mRecorder.setOutputFile('/sdcard/MYAUDIO.3gp')
self.mRecorder.setAudioEncoder(self.AudioEncoder.AMR_NB)
self.mRecorder.prepare()
I don't remember where I found the full code but if you need it, let me know. Thank you
You can use datetime for this.
from datetime import datetime
d = datetime.now()
d = d.strftime("%d_%m_%Y_%H%M%S")
self.mRecorder.setOutputFile('/sdcard/MYAUDIO_{}.3gp'.format(d))
Related
Hello does anyone has an idea why this piece of python code wont work for me.
I am trying to get a piece of api data into a python local variable and it just wont seem to work
.I tried different solutions
from urllib import request
import requests
import datetime
#******************************** VARIABLES *****************************#
SHEET_ENDPOINT = "https://api.sheety.co/c2508c72b1a9443966fca6445ff27747/workoutTracker/workouts"
#******************************** GET SHEETY API *****************************#
inbound = requests.get(SHEET_ENDPOINT)
RESULT = inbound.json()
drum = []
for exercise in RESULT["workouts"]:
date = exercise["date"]
time = exercise["time"]
exercise = exercise["exercise"]
duration = exercise["duration"]
calories = exercise["calories"]
g = f"Date:{date}\nTime:{time}\nExercise:{name}\nDuration:{duration}\nCalories:{calories}"
drum.append(g)
for item in drum:
print(item)
I am trying to build a calendar image using the package calendar-view. Below is the code from calendar-view:
from calendar_view.calendar import Calendar
from calendar_view.core import data
from calendar_view.core.event import Event
config = data.CalendarConfig(
lang='en',
title='Sprint 23',
dates='2019-09-23 - 2019-09-27',
show_year=True,
mode='working_hours',
legend=False,
)
events = [
Event('Planning', day='2019-09-23', start='11:00', end='13:00'),
Event('Demo', day='2019-09-27', start='15:00', end='16:00'),
Event('Retrospective', day='2019-09-27', start='17:00', end='18:00'),
]
data.validate_config(config)
data.validate_events(events, config)
calendar = Calendar.build(config)
calendar.add_events(events)
calendar.save("sprint_23.png")
This code works perfectly fine, however, I have been trying to build a similar calendar dynamically. As in, number events may increase or decrease. Is there a way to make this code behave more dynamic?
Here is some info on how I have tried making this dynamic:
I wrote the event details (shown below) in to a txt file.
[
Event('Planning', day='2019-09-23', start='11:00', end='13:00'),
Event('Demo', day='2019-09-27', start='15:00', end='16:00'),
Event('Retrospective', day='2019-09-27', start='17:00', end='18:00'),
]
Then read the text file in to the field 'event' (as shown below)
event = open("instruction.txt", "r")
But the code fails because it considers everything that has been read from file as a 'str'. Please see error below:
AttributeError: 'str' object has no attribute 'get_start_date'
You can use this code:
with open('instruction.txt', 'r') as file:
file_content = file.read()
events = eval(file_content)
eval() is the function that executes the string and returns the Python object.
I am trying to perform in-memory operations on files stored in azure datalake. I am unable to find documentation regarding using a matching pattern without using the ADL Downloader.
For a single file, this is the code I use
filename = '/<folder/<filename>.json'
with adlsFileSystemClient.open(filename) as f:
for line in f:
<file-operations>
But how do we filter based on filename (string matching) or based on last modified date.
When I used U-SQL , I had the option to filter the fileset based on the last modified option.
DECLARE EXTERNAL #TodaysTime = DateTime.UtcNow.AddDays(-1);
#rawInput=
EXTRACT jsonString string,
uri = FILE.URI()
,modified_date = FILE.MODIFIED()
FROM #in
USING Extractors.Tsv(quoting : true);
#parsedInput=
SELECT *
FROM #rawInput
WHERE modified_date > #TodaysTime;
Is there any similar options to filter the files modified during a specified period when using adlsFileSystemClient?
Github Issue: https://github.com/Azure/azure-data-lake-store-python/issues/300
Any help is appreciated.
Note:
This question was answered by akharit in GitHub recently. I am providing his answer below which solves my requirement.
**There isn't any in build functionality in the adls sdk itself as there is no server side api that will return only files modified with the last 4 hours.
It should be easy to write the code to do that after you get the list of all entries.
The modification time field returns milliseconds since unix epoch, which you can convert to a python datetime object by
from datetime import datetime, timedelta
datetime.fromtimestamp(file['modificationTime'] / 1000)
And then something like
filtered = [file['name'] for file in adl.ls('/', detail=True) if (datetime.now() - datetime.fromtimestamp(file['modificationTime']/1000)) > timedelta(hours = 4)]
You can use walk instead of ls for recursive enumeration as well.
**
Based on the below code , You can find the container level directories and file names with file properties including the last_modified data as well . So you can control the file based on the last_modified date .
from pyspark.sql.functions import col
from azure.storage.blob import BlockBlobService
from datetime import datetime
block_blob_service = BlockBlobService(account_name='acccount_name', account_key='account-key')
container_name ='Contaniner_name'
second_conatainer_name ='Contaniner_name_second'
#block_blob_service.create_container(container_name)
generator = block_blob_service.list_blobs(container_name,prefix="Recovery/")
report_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
myfile = open('/dbfs/adlsaudit/auditfiles2', 'w')
for blob in generator:
length = BlockBlobService.get_blob_properties(block_blob_service,container_name,blob.name).properties.content_length
last_modified = BlockBlobService.get_blob_properties(block_blob_service,container_name,blob.name).properties.last_modified
file_size = BlockBlobService.get_blob_properties(block_blob_service,container_name,blob.name).properties.content_length
# print("\t Recovery: " + blob.name,":" +str(length),":" + str(last_modified))
line = container_name+'|'+second_conatainer_name+'|'+blob.name+'|'+ str(file_size) +'|'+str(last_modified)+'|'+str(report_time)
myfile.write(line+'\n')
myfile.close()
I am trying to writing a program to read a configuration file but while testing it am having this error:
self.connection_attempts = self.config_file.get('CONNECTION_ATTEMPTS', 'TIME')
AttributeError: 'list' object has no attribute 'get'
I ma pretty sure it is something I don't get, but it is few hours I am trying to understand where the problem is.
My __init__ method looks like this:
import simpleconfigparser
class ReportGenerator:
def __init__(self):
self.config_parser = simpleconfigparser.configparser()
self.config_file = config_parser.read('config.ini')
self.connection_attempts = config_file.get('CONNECTION_ATTEMPTS', 'TIME')
self.connection_timeout = config_file.get('CONNECTION_TIMEOUT', 'TIMEOUT')
self.report_destination_path = config_file.get('REPORT', 'REPORT_PATH')
This code uses the SimpleConfigParser package.
You want config_parser.get() not config_file.get(). config_parser.read() simply returns the list of config files successfully read after populating the config object. (Usually it is called config or cfg, not config_parser).
This list (config_file) serves no purpose in your code and you might as well not capture it at all.
from simpleconfigparser import simpleconfigparser
TIME = 5
TIMEOUT = 10
REPORT_PATH = '/tmp/'
class ReportGenerator:
def __init__(self):
self.config = simpleconfigparser()
config.read('config.ini')
self.connection_attempts = config.get('CONNECTION_ATTEMPTS', TIME)
self.connection_timeout = config.get('CONNECTION_TIMEOUT', TIMEOUT)
self.report_destination_path = config.get('REPORT', REPORT_PATH)
My guess would also be, that you use the default value in .get() the wrong way, but i cannot be certain with the information you have given.
I'm trying to write a simple program to print the current date with Python 3.4. In the shell, I can import datetime, and use now() but when I write a script with a class it fails and gives this error:
"AttributeError: module object has no attribute now".
Can anyone help explain the problem? This is my code:
import datetime
class Date:
def __init__(self, filename):
self.writeToFile(filename)
def date(self):
now = datetime.datetime.now()
return now
def writeToFile(self, filename):
date = self.date()
file = open(filename, 'w')
file.write(date)
for i in range(20): # simply test for writting in file
file.write(str(i)+'\t')
file.close()
return file
d = Date('datetime.txt')
import datetime
datetime.datetime.now()
Make sure you are importing the intended datetime module, and it is not being overridden by local files with same name. you can check it with:
import datetime
print(datetime.__file__)
and check the output if it is pointing to the correct directory you want.
I had this error too and all I did was
Import datetime
from datetime import datetime
# then u can declare ur variable let's say something like
today = datetime.datetime.now()
#u can add what ever u want
#the point is make sure u do the datetime.datetime.now()
print(today)