|
What would you like to share?
Hello, I would like to ask a question, the size of the space occupied by python before executing file = f.read() is 84M, The size of the space occupied by python after execution is 2689MB, and the size of the file only is 1.27GB, why does it take up so much more space than the file size
|
|
|
|
|
Well, there isn't quite enough information here to answer your question in specifics, but the answer is related to the fact that python retains objects in memory for some time and files which are read in whole and assigned as a string to a variable can be doubled if you then perform an operation on that variable. So, it really depends on what you are doing after you read the file in with the file data.
So, you read that 1.27GB file into memory with file = f.read()
that means you just ate 1.27GB of memory + 84M you already used for the data.
Then you want to work on each line so you do f.splitlines() or something like that and you have just created another 1.27GB of data as it split each line of the file up. It didn't get rid of the original read data, it just duplicated it to split the lines.
ideally you should try
with open(filename, 'r'):
for line in f:
# do something
That will use a lot less memory as you iterate through the file to do what is necessary. Assuming that is an option.
|
|
|
|
|
This year politicians and economists have been invited to participate in Stanford Institute for Theoretical Economics (SITE 2021) conference as guests. You have to accommodate them in r rooms such that-
No room may remain empty.
A room may contain either only politicians or only economists, not both.
No economists are allowed to stay alone in a room.
Find the number of ways to place them. Note though, that all the rooms are identical. But each of the economists and politicians are unique.
Since the number of ways can be very large, print the answer modulo 998,244,353.
Input Format:
The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
The first and only line of each test case contains three space-separated integers p, q and r denoting the number of politicians, economists and rooms.
Output Format:
For each test case, output the number of ways to place them modulo 998,244,353.
Constraints
1≤T≤100
1≤p,q,r≤100
Sample Input:
4
2 1 4
2 4 4
2 5 4
2 8 4
Sample Output:
0
3
10
609
Explanation:
Test case 1: 4 rooms and 3 people. A room would remain empty.
Test case 2: There are 4 rooms and 6 people. There are 3 possible ways to distribute these 6 people in 3 rooms.
{Politician 1}, {Politician 2}, {Economist 1, Economist 2}, {Economist 3, Economist 4}
{Politician 1}, {Politician 2}, {Economist 1, Economist 3}, {Economist 2, Economist 4}
{Politician 1}, {Politician 2}, {Economist 1, Economist 4}, {Economist 2, Economist 3}
Please note that the rooms are identical.
|
|
|
|
|
You have to accommodate them. If you want help then show the code that you have written and explain what the problem is. But we will not write your assignment for you.
|
|
|
|
|
|
I am creating a bot (Python, aiogram) with a paid subscription using the Fondy payment gateway. I am using the principle of checking the payment status, but after sending the request, I receive the following error: "{'response': {'error_code': 1014, 'error_message': 'Invalid signature signature: 8dccc7a9e053c3cdc1efddb13f906bc866239ae6; response_signature_string: **********|1396424|85cceedc-e54a-43f5-a9fd-e39dc6f15c1a', 'request_id': 'ZKIukrDkwZDz9', 'response_status': 'failure'}}". Even though the order_id (85cceedc-e54a-43f5-a9fd-e39dc6f15c1a) is displayed correctly, the signature is still incorrect. Please help me find the error. Here is the payment code. <pre>0 @dp.message_handler(commands ='FONDY')
async def order_fondy(message: types.Message):
people_id = message.chat.id
base = sqlite3.connect('users.db')
cursor = base.cursor()
order_id = str(uuid.uuid4())
print(order_id)
cursor.execute(f"UPDATE users_id SET order_id == ? WHERE user_id == ?",
(order_id, people_id))
base.commit()
def generate_signature( order_id):
params1 = {
'merchant_id': '1396424',
'order_desc': 'demo order',
'currency': 'USD',
'amount': '125',
'order_id': f'{order_id}'
}
password = 'test'
# Generating a list of values sorted by key
values = []
for key in sorted(params1.keys()):
value = params1[key]
# Skipping empty values
if value:
values.append(str(value))
# Adding a parameter named "password" with the value 'test' to the beginning of the list of values
values.insert(0, password)
# Generating a string of values separated by a delimiter "|"
signature_string = "|".join(values)
print(signature_string)
# Applying the SHA1 function to a string and returning the result in the format of a hexadecimal string
signature = hashlib.sha1(signature_string.encode('utf-8')).hexdigest()
return signature
signature = generate_signature(order_id)
print(signature) # Displaying the signature
api = Api(merchant_id=1396424,
secret_key='test')
checkout = Checkout(api=api)
data = {
'order_desc': 'demo order',
'currency': 'USD',
'amount': '125',
'order_id': f'{order_id}'
}
payment_url = checkout.url(data).get('checkout_url')
# Sending payment links to the user
await message.reply(f"To make a payment, please follow the link: {payment_url}")
# Checking the payment status
@dp.callback_query_handler(text='check')
async def check_payment_status(call: CallbackQuery):
people_id = call.message.chat.id
connect = sqlite3.connect('users.db')
cursor = connect.cursor()
cursor.execute(f"SELECT order_id FROM users_id WHERE user_id = {people_id}")
result_1 = cursor.fetchone()
order_id = str(result_1[0])
print(order_id)
cursor.execute(f"SELECT payment_id FROM users_id WHERE user_id = {people_id}")
result_2 = cursor.fetchone()
signature = str(result_2[0])
print(signature)
url = 'https://pay.fondy.eu/api/status/order_id'
headers = {'Content-Type': 'application/json'}
data = {
"request": {
'currency': 'USD',
'amount': '125',
'order_id': f'{order_id}',
'merchant_id': '1396424',
'signature': f'{signature}',
'order_desc': 'demo order',
"response_url": "https://pay.fondy.eu/responsepage/"
}
}
async with aiohttp.ClientSession() as session:
async with session.post(url, headers=headers, data=json.dumps(data)) as response:
response_data = await response.json()
print(response_data)
if response.status == 200:
# Handling a successful response</pre>
|
|
|
|
|
|
|
Googling returns several results
fondy "1014" "invalid signature"
As a guess by briefly looking at the github example first page and from past experience with past services I am guessing the following. (Pretty strong guess.)
1. The 'signature' is generated based on the input that you are pushing in the request.
2. You are generating the signature incorrectly based on what you are actually sending.
If my guess is correct then you need to more closely read what the 'signature' means and how to generate it.
The github link in the above google search suggests and exact algorithm.
|
|
|
|
|
|
Based on Accept purchase - FONDY Documentation[^] I think you are not setting the signature correctly.
based on your code you set signature wrong, I think, it's hard to read. They are expecting a sha1 hash of password|amount|currency|merchant_id|order_id|order_desc. Maybe I am not reading your post correctly because it is all lumped together, but it seems to me that the problem is the signature is not being set to a sha1 hash of those items based on result_2 = cursor.fetchone() and signature = str(result_2[0]) it is set to payment_id.
|
|
|
|
|
which programming language is much better in AI
|
|
|
|
|
The one that creates the best answers.
|
|
|
|
|
I want to learn Python but I don't know where to start to study any programmer to help me. I tried to study on my own but it was too heavy for me.(Quiero aprender Python pero no se por donde empezar a estudiar algun programador que me ayude. Traté de estudiar por mi cuenta, pero era demasiado pesado para mí)
|
|
|
|
|
The best ways to get started are in order:
1) Go on a course. The material will be structured and organised so you get introduced to every part of the language provided you do all the exercises. In addition, if you don;t understand something, a human tutor can explain in different ways until you do.
2) Get a book. The material will be structured and organised so you get introduced to every part of the language provided you do all the exercises. Wrox an Addison Wesley do good ones on most programming languages.
3) Guess, and try to work it out on your own. You might learn something, but it probably won't be what you should be learning. Very poor method, you don't get introduced to anything that you haven't met ...
4) Grab other peoples code and try to understand it. You will learn nothing other than some people release code that doesn't actually work ...
5) Youtube videos. Generally produced by people with no idea how to make a video, how to teach, and most cases how to code; produced for the likes and subscribes as these monetize the channel. There are probably some good ones out there, but they are buried under a sea of rubbish so nobody is likely to find them.
Would I start with Python? No - I'd start with a compiled language like C#, because at least you have to get all the silly syntax errors out of the way before you can run your code. With an interpreted language like Python, something you wrote 2 weeks ago can fail to run when you add new code because that part had never actually been executed before.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
|
Rookie question:
Imagine a script that replaces the vowels of curse words with *. Is it possible to match the curse words without case sensitivity but retain the case in the replacement so that the original case format (tone) is retained?
|
|
|
|
|
|
Try this:
table = str.maketrans('aeiouAEIOU', '**********')
for curseword in listofwords:
clearword = curseword.translate(table)
print(clearword)
|
|
|
|
|
Hi All,
Partial Functions I'm sorry can't see any point in them.
from functools import partial
def multiply(x, y):
return x * y
dbl = partial(multiply, 2)
print(dbl(4))
is that supposed to better than multiply(4,2)? or is just a bad example???
|
|
|
|
|
|
Hmmm, a feature that you need on occasions for runtime..?
|
|
|
|
|
I have still not come across a problem that 'needs' this feature. But maybe that's because my blue sky thinking has so many clouds in it.
|
|
|
|
|
|
|