جميل جدا ArijTrabelsi
class Post:
def __init__(self, id, photo_url, name, body):
self.id = id
self.photo_url = photo_url
self.name = name
self.body = body
class PostStore:
def get_all(self):
return posts
def add(self, post):
posts.append(post)
def get_by_id(self, id):
for post in posts:
if post.id == id:
return post
return None
def update(self, id, fields):
post = self.get_by_id(id)
post.name = fields["name"]
post.photo_url = fields["photo_url"]
post.body = fields["body"]
return post
def delete(self, id):
posts.remove(self.get_by_id(id))
posts = []
عمل رائع يا ابرار
class n:
def __init__(self ,a ,b , c,z):
self.a = a
self.b = b
self.c = c
self.z = z
store =[]
class C:
def get_all(self):
return store
def add(self , o):
store.append(o)
return store
def get_by_a(self ,a):
total = None
for n in store:
if n.a == a:
total = n
return total
def updat(self, a ,new):
fa =self.get_by_a(a)
fa.a =new['a']
fa.b =new['b']
fa.c =new['c']
fa.z new['z']
def remove(self, a ):
fa = self.get_by_a(a)
store.remove(fa)
-
بقي معي اخر return لم اكتبه لأنني لاعلم ماذا يعمل
-
ملحوظة : غيرت المسميات بمسميات من عندي لكي افهم الكود
كتبت كل كود بفهم ومعرفة وظيفته
لم اكتب الكود من اول لحظة
بل استغرق الوضع ثلاث ايام لفهم الحلول السابقه
ثم قمت بتطبيقة وتغيير المسميات وكتابته حرف حرف
ولم يروق لي كتابته بنفس المسميات
لانه قبلي 10 حلول بنفس النمط والمسمى
شكراً لكم
من الان اجازه اسبوع
اعوض عن الايام السابقة
ويعلم الله ايش القادم …
class Post:
def __init__(self, id, photo_url, name, body):
self.id = id
self.photo_url = photo_url
self.name = name
self.body = body
posts =[]
class PostStore:
def get_all(self):
return posts
def add(self, post):
posts.append(post)
def get_by_id(self, id):
for post in posts:
if post.id == id:
return post
#break
def update(self, id, fields):
post = self.get_by_id(id)
post.name=fields['name']
post.body=fields['body']
post.photo_url =fields['photo_url']
def delete(self, id):
posts.remove(self.get_by_id(id))
عمل جيد يا بسام
إذا كنت تقصد return في هذا الكود:
def get_by_a(self ,a):
total = None
for n in store:
if n.a == a:
total = n
return total
فهي تقوم بارجاع العنصر الذي تم ايجاده داخل الحلقة
هناك بعض الملاحظات:
-
المفترض أن تكون اسامي المتغيرات تدل على معناها، مثلا total يفضل أن تجعله result، لأن الدالة get_by_a تبحث عن عنصر وترجعه بعد ايجاده (وليست عملية جمع ونقوم بايجاد المجموع total)
-
يفضل أن تبتعد عن الاسامي المبهمة مثل a b c d وأن تكون تدل على ما تقوم به، مثلا الاسم بدل a تكتب name وهكذا
-
داخل الحلقة for loop:
for n in store:
if n.a == a:
total = n
يفضل أن تقوم بارجاع العنصر مباشرة return total أو تقوم بعمل break لايقاف الحلقة (لأنك وجدت العنصر ولا داعي لمتابعة عملية البحث)
أكثر شيء أعجبني أنك قمت بالتلاعب بالكود لكي تعرف بالضبط بما يقوم به، تحية لك على همتك
عمل مضبوط يا حسين
PostStore
class Post:
def __init__(self, id, photo_url, name, body):
self.id = id
self.photo_url = photo_url
self.name = name
self.body = body
posts = []
class PostStore:
def get_all(self):
# get all posts
return posts
def add(self, post):
# append post
posts.append(post)
def get_by_id(self, id):
# search for post by
result = None
for item in posts:
if item.id == id:
result = item
break
return result
def update(self, id, fields):
# update post data
for i in range(len(posts)):
if posts[i].id == id:
posts[i].photo_url = fields["photo_url"]
posts[i].name = fields["name"]
posts[i].body = fields["body"]
break
def delete(self, id):
# delete post by id
result = None
for i in range(len(posts)):
if posts[i].id == id:
result = i
break
if result == None:
print("Post not found!")
else:
posts.remove(posts[result])
Testing
from PostStore import *
store = PostStore()
p1 = Post(1, "fghj", "Hassan", "okkkkkkkay")
p2 = Post(2, "hdfsv", "Zekkouri", "hhhhhhhh ")
p3 = Post(3, "bcvjkl", "Zeek Zone", "finaally")
store.add(p1)
store.add(p2)
store.add(p3)
print("Geting all:")
result = store.get_all()
for item in result:
print(item.id, item.name, item.body)
print("_______________________________")
print("Geting id = 3:")
result = store.get_by_id(3)
if result == None:
print("Post not found!")
else:
print(result.id, result.name, result.body)
print("_______________________________")
print("Edeting 1:")
updated_fields = {'name': 'Maryam',
'photo_url': 'https://images.pexels.com/photos/736716/pexels-photo-736716.jpeg?auto=compress&cs=tinysrgb&dpr=1&h=100&w=100',
'body': 'Lorem Ipsum'}
store.update(1, updated_fields)
print("Geting id = 1:")
result = store.get_by_id(1)
if result == None:
print("Post not found!")
else:
print(result.id, result.name, result.body)
print("_______________________________")
print("Deleting 2")
store.delete(2)
print("After Deletion:")
print("Geting all:")
result = store.get_all()
for item in result:
print(item.id, item.name, item.body)
print("_______________________________")
مـهـمـة إنـهـاء مـسـتـودع لـلـمـنـشـورات …
class Post:
def __init__(self,id,name,photo_url,body):
self.id=id
self.name=name
self.photo_url=photo_url
self.body=body
posts=[]
class PostStore:
def get_all(self):
return posts
def add(self,post):
posts.append(post)
return posts
def get_by_id(self,id):
result=None
for post in posts:
if post.id==id:
result=post
break
return result
def update(self,id,fields):
post=self.get_by_id(id)
post.name=fields["name"]
post.photo_url=fields["photo_url"]
post.body=fields["body"]
return post
def delete(self,id):
post=self.get_by_id(id)
posts.remove(post)
return posts
عمل مضبوط يا فاتن كالعادة
واجهت رسالة خطأ عند دالة الحذف
class Post:
def __init__(self, id, photo_url, name, body):
self.id = id
self.photo_url = photo_url
self.name = name
self.body = body
posts = []
class PostStore:
def get_all(self):
#get all posts
return posts
def add_post(self, post):
#add new post
posts.append(post)
def get_by_id(self, id):
#get post by id
result = None
for post in posts:
if post.id == id:
result = post
break
return result
def update(self, id, fields):
post = self.get_by_id(id)
post.photo_url = fields['photo_url']
post.name = fields['name']
post.body = fields['body']
def delete(self, id):
post = self.get_by_id(id)
posts.remove(post)
error message is
File "C:\Users\IBM\Desktop\tests\store.py", line 42
posts.remove(post)
^
TabError: inconsistent use of tabs and spaces in indentation
[Finished in 0.6s with exit code 1]
عمل ممتاز اخي
بالنسبة المشكله اعتقد انك خلط بين ال tabs و ال spaces غيرها كلها ل spaces وتفادى ال tabs
class Post:
def __init__(self, id, photo_url, name, body):
self.id = id
self.photo_url = photo_url
self.name = name
self.body = body
class PostStore:
def __init__(self):
self.posts=[]
def get_all(self):
# get all posts - الحصول على كل المنشورات
return self.posts
def add(self, post):
# append post - إضافة منشور
(self.posts).append(post)
def get_by_id(self, id):
# search for post by id - id البحث عن منشور بالمعرف
result=None
for post in self.posts:
if id==post.id:
result=post
break
# print( result)
return result
def update(self, id, fields):
# update post data - id تعديل منشور بالمعرف
for post in self.posts:
if id==post.id:
post.name=fields['name']
post.photo_url=fields['photo_url']
post.body=fields['body']
def delete(self, id):
# delete post by id - id حذف منشور بالمعرف
for post in self.posts:
if id==post.id:
(self.posts).remove(post)
class Post:
def __init__(self, id, photo_url, name, body):
self.id = id
self.photo_url = photo_url
self.name = name
self.body = body
posts = []
class PostStore:
def get_all(self):
return posts
def add(self, post):
posts.append(post)
def get_by_id(self, id):
result = None
for post in posts:
if post.id == id:
result = post
break
return result
def update(self, id, fields):
post = self.get_by_id(id)
post.photo_url = fields['photo_url']
post.name = fields['name']
post.body = fields['body']
def delete(self, id):
post = self.get_by_id(id)
posts.remove(post)
class Post:
def __init__(self, id, photo_url, name, body, post):
self.id = id
self.photo_url = photo_url
self.name = name
self.body = body
self.post=post
posts = []
class PostStore:
def get_all(self):
return (posts)
def add(self, post):
posts.append(self.post)
print(posts)
return(posts)
def get_by_id(self, id):
result = None
for post in posts:
if post.id == id:
result = post
break
return result
def update(self,id,fields):
post = self.get_by_id(id)
post.name = fields["name"]
post.photo_url = fields["photo_url"]
post.body =fields["body"]
return post
def delete(self,id):
post = self.get_by_id(id)
posts.remove(post)
return posts
obj=PostStore()
obj.post=(input())
obj.add(obj.post)
#obj.get_by_id(3)