473,671 Members | 2,340 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Searching for some kind of data type

Hi,
for an FTP server I wrote I'd need to group the FTP commands in one
table that defines the command itself, the syntax string, required
permission, whether it requires authorization, whether it takes
argument and whether there's a need to validate the path from the
argument.
The more obvious way I found to do that is something like this:

class CommandProperty :
def __init__(self, perm, auth_needed, arg_needed, check_path,
syntax):
self.perm = perm
self.auth_neede d = auth_needed
self.arg_needed = arg_needed
self.check_path = check_path
self.syntax = syntax

ftp_cmds = {
"ABOR" : CommandProperty (perm=None, auth_needed=Tru e,
arg_needed=Fals e, check_path=Fals e, syntax="ABOR (abort transfer)."),
"APPE" : CommandProperty (perm='a', auth_needed=Tru e,
arg_needed=True , check_path=True , syntax="APPE <SPfile-name
(append data to an existent file)."),
"CDUP" : CommandProperty (perm='e',
auth_needed=Tru e,arg_needed=Fa lse, check_path=Fals e, syntax="CDUP (go
to parentdirectory )."),
...
...
...
}

....but I find it somewhat redundant and... "ugly".
I was wondering if there was some kind of data type which could better
fit such purpose or if someone could suggest me some other approach to
do this same thing. Maybe using a dictionary is not the better choice
here.
Thanks in advance
--- Giampaolo
http://code.google.com/p/pyftpdlib/
Aug 2 '08 #1
4 1316
Giampaolo Rodola' wrote:
Hi,
for an FTP server I wrote I'd need to group the FTP commands in one
table that defines the command itself, the syntax string, required
permission, whether it requires authorization, whether it takes
argument and whether there's a need to validate the path from the
argument.
The more obvious way I found to do that is something like this:

class CommandProperty :
def __init__(self, perm, auth_needed, arg_needed, check_path,
syntax):
self.perm = perm
self.auth_neede d = auth_needed
self.arg_needed = arg_needed
self.check_path = check_path
self.syntax = syntax

ftp_cmds = {
"ABOR" : CommandProperty (perm=None, auth_needed=Tru e,
arg_needed=Fals e, check_path=Fals e, syntax="ABOR (abort transfer)."),
"APPE" : CommandProperty (perm='a', auth_needed=Tru e,
arg_needed=True , check_path=True , syntax="APPE <SPfile-name
(append data to an existent file)."),
"CDUP" : CommandProperty (perm='e',
auth_needed=Tru e,arg_needed=Fa lse, check_path=Fals e, syntax="CDUP (go
to parentdirectory )."),
...
...
...
}

...but I find it somewhat redundant and... "ugly".
I was wondering if there was some kind of data type which could better
fit such purpose or if someone could suggest me some other approach to
do this same thing. Maybe using a dictionary is not the better choice
here.
Thanks in advance
--- Giampaolo
http://code.google.com/p/pyftpdlib/

Seems completely reasonable to me. You might just consider using keyword
arguments in the __init__ method and eliminate the dictionary altogether.

Not tested, but you will get the idea:

class CommandProperty :
def __init__(self, perm = perm, auth_needed = True, arg_needed = True,
check_path = False, syntax = syntax):

self.perm = perm
self.auth_neede d = auth_needed
self.arg_needed = arg_needed
self.check_path = check_path
self.syntax = syntax

ftpCommands = dict(
ABOR = CommandProperty (perm = None, arg_needed = False,
syntax="ABOR (abort transfer)."),
APPE = CommandProperty (perm = 'a', check_path=True ,
syntax = "APPE <SPfile-name (append data to" \
"an existent file)."),
CDUP = CommandProperty (perm= 'e', arg_needed = False,
syntax="CDUP (goto parentdirectory )."),
....
....
....
)
IMHO this is a "little" easier to manage because you can take advantage of the
default values for keyword arguments to eliminate some of the arguments.

Hope this helps,
Larry
Aug 2 '08 #2
Larry Bates wrote:
Giampaolo Rodola' wrote:
>Hi,
for an FTP server I wrote I'd need to group the FTP commands in one
table that defines the command itself, the syntax string, required
permission, whether it requires authorization, whether it takes
argument and whether there's a need to validate the path from the
argument.
The more obvious way I found to do that is something like this:

class CommandProperty :
def __init__(self, perm, auth_needed, arg_needed, check_path,
syntax):
self.perm = perm
self.auth_neede d = auth_needed
self.arg_needed = arg_needed
self.check_path = check_path
self.syntax = syntax

ftp_cmds = {
"ABOR" : CommandProperty (perm=None, auth_needed=Tru e,
arg_needed=Fal se, check_path=Fals e, syntax="ABOR (abort transfer)."),
"APPE" : CommandProperty (perm='a', auth_needed=Tru e,
arg_needed=Tru e, check_path=True , syntax="APPE <SPfile-name
(append data to an existent file)."),
"CDUP" : CommandProperty (perm='e',
auth_needed=Tr ue,arg_needed=F alse, check_path=Fals e, syntax="CDUP (go
to parentdirectory )."),
...
...
...
}

...but I find it somewhat redundant and... "ugly".
I was wondering if there was some kind of data type which could better
fit such purpose or if someone could suggest me some other approach to
do this same thing. Maybe using a dictionary is not the better choice
here.
Thanks in advance
--- Giampaolo
http://code.google.com/p/pyftpdlib/


Seems completely reasonable to me. You might just consider using
keyword arguments in the __init__ method and eliminate the dictionary
altogether.

Not tested, but you will get the idea:

class CommandProperty :
def __init__(self, perm = perm, auth_needed = True, arg_needed =
True,
check_path = False, syntax = syntax):

self.perm = perm
self.auth_neede d = auth_needed
self.arg_needed = arg_needed
self.check_path = check_path
self.syntax = syntax

ftpCommands = dict(
ABOR = CommandProperty (perm = None, arg_needed = False,
syntax="ABOR (abort transfer)."),
APPE = CommandProperty (perm = 'a', check_path=True ,
syntax = "APPE <SPfile-name (append data to" \
"an existent file)."),
CDUP = CommandProperty (perm= 'e', arg_needed = False,
syntax="CDUP (goto parentdirectory )."),
...
...
...
)
How does this strike you? With care, the comment and the table could
be kept aligned and nicely readable

cmd_data = dict(
# cmd = (perm, auth, arg, path, syntax),
ABOR = (None, False, False, False, "ABOR (abort transfer)."),
APPE = (None, False, False, True, "APPE <SPfile-name (append data to"),
...
]

ftpCommands = {}
for cmd,args in cmd_data.iterit ems():
ftpCommands[cmd] = CommandProperty (*args)

Gary Herron
>

IMHO this is a "little" easier to manage because you can take
advantage of the default values for keyword arguments to eliminate
some of the arguments.

Hope this helps,
Larry
--
http://mail.python.org/mailman/listinfo/python-list
Aug 2 '08 #3
On 2 Ago, 18:18, Gary Herron <gher...@island training.comwro te:
Larry Bates wrote:
Giampaolo Rodola' wrote:
Hi,
for an FTP server I wrote I'd need to group the FTP commands in one
table that defines the command itself, the syntax string, required
permission, whether it requires authorization, whether it takes
argument and whether there's a need to validate the path from the
argument.
The more obvious way I found to do that is something like this:
class CommandProperty :
* * def __init__(self, perm, auth_needed, arg_needed, check_path,
syntax):
* * * * self.perm = perm
* * * * self.auth_neede d = auth_needed
* * * * self.arg_needed = arg_needed
* * * * self.check_path = check_path
* * * * self.syntax = syntax
ftp_cmds = {
* * "ABOR" : CommandProperty (perm=None, auth_needed=Tru e,
arg_needed=Fals e, check_path=Fals e, syntax="ABOR (abort transfer)."),
* * "APPE" : CommandProperty (perm='a', *auth_needed=Tr ue,
arg_needed=True , *check_path=Tru e, *syntax="APPE <SPfile-name
(append data to an existent file)."),
* * "CDUP" : CommandProperty (perm='e',
auth_needed=Tru e,arg_needed=Fa lse, check_path=Fals e, syntax="CDUP (go
to parentdirectory )."),
* * ...
* * ...
* * ...
* * }
...but I find it somewhat redundant and... "ugly".
I was wondering if there was some kind of data type which could better
fit such purpose or if someone could suggest me some other approach to
do this same thing. Maybe using a dictionary is not the better choice
here.
Thanks in advance
--- Giampaolo
http://code.google.com/p/pyftpdlib/
Seems completely reasonable to me. *You might just consider using
keyword arguments in the __init__ method and eliminate the dictionary
altogether.
Not tested, but you will get the idea:
class CommandProperty :
* * def __init__(self, perm = perm, auth_needed = True, arg_needed =
True,
* * * * * * * * *check_path = False, syntax = syntax):
* * * * self.perm = perm
* * * * self.auth_neede d = auth_needed
* * * * self.arg_needed = arg_needed
* * * * self.check_path = check_path
* * * * self.syntax = syntax
ftpCommands = dict(
* * ABOR = CommandProperty (perm = None, arg_needed = False,
* * * * * * *syntax="ABOR (abort transfer)."),
* * APPE = CommandProperty (perm = 'a', *check_path=Tru e,
* * * * * * *syntax = "APPE <SPfile-name (append datato" \
* * * * * * * * * * * "an existent file)."),
* * CDUP = CommandProperty (perm= 'e', arg_needed = False,
* * * * * * *syntax="CDUP (goto parentdirectory )."),
...
...
...
* * )

How does this strike you? * *With care, the comment and the table could
be kept aligned and nicely readable

cmd_data = dict(
*# cmd = (perm, auth, * arg, path, * syntax),
*ABOR = (None, False, False, False, "ABOR (abort transfer)."),
*APPE *= (None, False, False, True, *"APPE <SPfile-name (appenddata to"),
*...
]

ftpCommands = {}
for cmd,args in cmd_data.iterit ems():
* * ftpCommands[cmd] = CommandProperty (*args)

Gary Herron


IMHO this is a "little" easier to manage because you can take
advantage of the default values for keyword arguments to eliminate
some of the arguments.
Hope this helps,
Larry
--
http://mail.python.org/mailman/listinfo/python-list- Nascondi testo citato

- Mostra testo citato -- Nascondi testo citato

- Mostra testo citato -
Thanks, I didnt' know dict() could be used with =.
I think I'm going to use this solution.
--- Giampaolo
http://code.google.com/p/pyftpdlib/
Aug 2 '08 #4
On Aug 2, 1:12*pm, "Giampaolo Rodola'" <gne...@gmail.c omwrote:
Hi,
for an FTP server I wrote I'd need to group the FTP commands in one
table that defines the command itself, the syntax string, required
permission, whether it requires authorization, whether it takes
argument and whether there's a need to validate the path from the
argument.
The more obvious way I found to do that is something like this:

class CommandProperty :
* * def __init__(self, perm, auth_needed, arg_needed, check_path,
syntax):
* * * * self.perm = perm
* * * * self.auth_neede d = auth_needed
* * * * self.arg_needed = arg_needed
* * * * self.check_path = check_path
* * * * self.syntax = syntax

ftp_cmds = {
* * "ABOR" : CommandProperty (perm=None, auth_needed=Tru e,
arg_needed=Fals e, check_path=Fals e, syntax="ABOR (abort transfer)."),
* * "APPE" : CommandProperty (perm='a', *auth_needed=Tr ue,
arg_needed=True , *check_path=Tru e, *syntax="APPE <SPfile-name
(append data to an existent file)."),
* * "CDUP" : CommandProperty (perm='e',
auth_needed=Tru e,arg_needed=Fa lse, check_path=Fals e, syntax="CDUP(go
to parentdirectory )."),
* * ...
* * ...
* * ...
* * }

...but I find it somewhat redundant and... "ugly".
I was wondering if there was some kind of data type which could better
fit such purpose or if someone could suggest me some other approach to
do this same thing. Maybe using a dictionary is not the better choice
here.
How about something like this? The idea is to summarize the command
table, then construct the ftp command dictionary programatically from
it.

cmd_table = dict(
ABOR='-AR- (abort transfer)',
APPE='aARP <SPfile-name (append data to a file)',
CDUP='eA-- (go to parent directory)',
...
)

The first block of four characters give the options: permission,
whether authentication is needed, whether the argument is required,
and whether the path needs checking. Permission is the permission
letter or - (for none), A or - for authentication, R or - for argument
required, and - or P for path required. Then the help text follows
(with the command name omitted since it's redundant).

Turning these into CommandProperty 's is straightforward (untested
code):

ftp_commands = {}
for cmd, descriptor in cmd_table.iteri tems():
options, help = descriptor.spli t(' ', 1)
options = [None if opt == '-' else opt for opt in options]
perm, auth, arg, path = options
assert perm is None or perm in 'ae...'
assert auth is None or auth == 'A'
assert arg is None or arg == 'R'
assert path is None or path == 'P'
ftp_commands[cmd] = CommandProperty (perm=perm, auth_required=a uth,
arg_required=ar g, check_path=path , syntax = '%s %s' % (cmd, help))

--
Paul Hankin
Aug 2 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2322
by: Joh | last post by:
Hello, (sorry long) i think i have missed something in the code below, i would like to design some kind of detector with python, but i feel totally in a no way now and need some advices to advance :( data = "it is an <atag> example of the kind of </atag> data it must handle and another kind of data".split(" ")
13
2302
by: gtux | last post by:
Hi everybody: I'm new in Javascript, I found some code and there is this: var fruit = { 'apple' : { 'weight' : 10, 'cost' : 9}, 'peach' : { 'weight' : 19, 'cost' : 10} }
2
1496
by: Matias | last post by:
Hi: I need some help, about How can I define a GLOBAL Data type in ASP.net proyect. thanks
10
3222
by: mattmerc | last post by:
Hi all, I posted this about a week back with no response so I want to try again. I have a weird questions in regards to working with sql image data types. I know how to take the image type and convert it into a picture on the screen. What I have is a situation where I need to read the image data from one database, store it, and then upload it to another database. I tried storing it in viewstate and in a byte variable. I don't know if I...
5
3398
by: rAinDeEr | last post by:
Hi, I have a web application with a table to store terms and conditions of a Company. This may some times run into many pages and some times it may be just a few sentences. It is a character text field. I want to know which Data type I need to use so that it doesnt waste memory. thanks in advance, rAinDeEr
1
9909
by: neeraj | last post by:
Hi All Can any give me the code for convert "DataColumn" data type of "DataTable". Even if data table already populated (have data) Actually I am creating one search module which searches the data from "DataView" using "RowFilter" method. Here I m using like operator for searching, its work fine for string but if data type of "Data Column" is Date or Integer then its not working. Now I m decide I will convert data types of all column...
35
15243
by: dtschoepe | last post by:
Greetings. I am working on an assignment and can't seem to get the right concept for something I'm attempting to do with enum data types. I have defined the following in my code: enum color {red, blue, green, yellow, black, purple, pink}; What I am doing now is reading from a text file a string (char *). I
5
1984
by: isoquin | last post by:
The below code throws the error: "Data type mismatch in criteria expression" Searching online didn't seem to offer me a solution. Public Sub update_by_EDD(myDate As Date, myMR As String) Dim rs As Object Set rs = Me.Recordset.Clone ' strCriteria = "( = '" & myMR & "')" strCriteria = "( = '" & myDate & "')" rs.FindFirst (strCriteria)
0
1096
by: Adam Chapman | last post by:
Hi, Im completely new to c++ and I am trying to interface some c++ source code with matlab. Mt question is: I there a way to find the data type of a variable other than searching through the code to see where it was defined? What I would like to do is use a printf statement to show the type of a certain variable.
0
8473
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8390
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8819
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8597
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8667
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7428
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5692
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4402
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2048
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.