哦不,我的代码正在发布......帮助! [英] Oh no, my code is being published ... help!

查看:64
本文介绍了哦不,我的代码正在发布......帮助!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不时经常会有一个Linux论坛,我提到了我写的几个脚本。一本小小的b $ b Linux杂志的编辑听到并发现它们很有趣,可以让我写一篇关于它们的文章。当然,我很高兴地接受了。我在文章中写了

并提交了它,我被告知要在1月份的
问题上寻找它。听起来不错吧?


事情是我开始对此感到有些紧张。你看,

编程不是我的全职工作。正如他们所说的那样,我从时间开始涉足到时间,主要是为了刮擦我自己的痒。但是,我知道

我的代码可能远非专业品质。所以,我有点想知道你们中的一些人是否有兴趣巅峰,而且b $ b提供了一些改善质量和安全性的建议。

代码。让我试着解释一下他们做了什么。


让我们说,例如,你有一个大的目录树

你想要压缩包含您几乎不使用的数据,

但您希望可以随时轻松访问。在我的

的情况下,该目录树包含来自我的DSLR相机的
的RAW图像文件。每个文件大约10 MB。该目录树的总大小

大约是45 GB,并且它在不断增长。

(注意:我将完成的,已处理的图像存储在不同的

目录树。它们存储为JPEG文件,所以它们已经压缩了。)你将如何使用压缩来重新获得一些
$ b在这种情况下$ b磁盘空间?


好​​吧,我提出的一种方法就是编写自己的工具来完成这项工作。

我创建了一个名为7sqz(7Squeeze)的程序,可以轻松处理这个

任务。它是一个Python脚本,它通过仅压缩其内容的

目录树进行导航,而不是实际的

目录。当它进入树上的每个目录时,它会将该目录上的所有

文件保存在同一目录上的存档上,并为其指定目录本身的名称。如果它发现目录

已经有一个具有正确名称的存档文件,那就单独留下

并转到下一个目录,除非它还找到一个未压缩的

文件。当发生这种情况时,它只是将其移动到现有的

存档文件中,如果它已经存在则在存档中更新它。


我还创建了7usqz,这是与7sqz相反的对应物。它只需要通过一个指定的目录树查找存档

文件命名为保存目录并解压缩它们,

基本上离开了在被挤压之前的目录。

7sqz和7usqz都使用p7zip进行实际压缩,所以你需要

才能安装p7zip。


您可以从这里获得7sqz:
http:// rmcorrespond .googlepages.com / 7sqz


你可以从这里得到7usqz:
http://rmcorrespond.googlepages.com/7usqz


下载后,将它们保存在/中usr / bin和make

确定它们是可执行的。


要使用7sqz,你可以给它一个目标目录作为参数,

是这样的:


7sqz / home / some_dire ctory


默认情况下,它会使用7z格式(比zip更好地压缩

),但如果您愿意,可以使用zip格式这样的 -

m选项:


7sqz -m zip / home / some_directory


默认情况下它会使用Normal作为压缩级别,但是你可以使用EXTRA或MAX,如果你喜欢使用这样的-l选项:


7sqz -l额外/ home / some_directory


默认情况下,如果在

压缩过程中发现错误,它将跳过任何文件并记录错误,但你可以告诉它停止在

错误使用-e选项如下:


7sqz -e / home / some_directory


当然,您可以随意组合选项像这样:


7sqz -m zip -l Max -e / home / some_directory


正如我所说,7usqz是对面的7sqz。使用它你

可以给它一个目标目录作为参数,如下所示:


7usqz / home / some_directory

默认情况下,如果在

解压缩期间发现错误,它将跳过任何文件并记录错误,但您可以告诉它停止

错误使用-e选项是这样的:


7usqz -e / home / some_directory


请做一些,或者更好的很多测试,然后在你无法承受的

目录上使用它。我相信它有所有必要的安全预防措施来保护你的数据,但是我不能保证它是b
。这就是为什么我要求你的帮助。我所能说的只是

,我从来没有丢失任何数据,而且它对我来说非常好。

我。您怎么看?

解决方案

rm< rm ********** @ gmail.comwrote:
< blockquote class =post_quotes>
>
事情是我开始对此感到有些紧张。你看,
编程不是我的全职工作。正如他们所说,我从时间开始涉足时间,主要是为了抓住我自己的痒。但是,我知道我的代码可能远非专业品质。所以,我想知道你们中有些人是否有兴趣巅峰,并提出一些改善
代码质量和安全性的建议。让我试着解释一下他们做了什么。



有几个地方你可以这样做:

strng =" abcde"

print str(strng)


为什么调用str()?这些已经是字符串,即使它们不是b $ b,也不是打印。将转换其论点。


您还有几个实例:

print(Error Squeezing%s ...)


括号在这里没有任何意义,并且是单一的。


你有:

如果ErrorHalt == True:

几乎在每种情况下,最好把它写成:

如果ErrorHalt:


这同样适用于:

如果调试== 1:

这实际上是一个布尔值,大多数人会把它写成:

如果调试:


元组用一个p拼写,但那个'非常挑剔...


请注意:

def保存(个体经营):

LogFile = open(self.logpath," w")

for self.LogStrings:

LogFile.write(line)

LogFile.close()

可以写成:

def保存(个体经营):

open( self.logpath,w,. writelines(self.LogStrings)

但这真的是微优化。


大多数人都会争辩应该写入日志文件中的每一行并立即刷新到磁盘上,直到稍后才保存。如果您的

程序意外终止,您的所有日志信息都将丢失。


您认为p7zip将安装在/ usr / bin / 7za中。这不好。

在我的许多系统上,我将所有新软件包安装到/ usr / local / bin中。一些

的人使用/ opt。你应该检查PATH以寻找7za。

-

Tim Roberts, ti * *@probo.com

Providenza& Boekelheide,Inc。


Tim Robertsaécrit:


rm< rm ****** **** @ gmail.comwrote:



(snip)


你还有几个实例:

print(Error Squeezing%s ...)


圆括号在这里没有用处,而且是单一的。



和FWIW,错误消息应该发送到sys.stderr,而不是sys.stdout

这是正常程序输出。


=>

#断言您之前导入的sys当然是

print> sys.stderr," Error yadda yadda %s %any


2007年11月30日上午1:19,Tim Roberts< ti ** @ probo.comwrote:


你还有几个例子:

print(Error Squeezing%s ...)


圆括号在这里没有任何目的,并且是单一的。



我认为最终会删除''print''作为在
Python 3中的声明,就是这样写的(好像它是一个打印功能)

首选,因为转换的次数会少一些。


-

#pd


There is a Linux forum that I frequent from time to time on which I
mentioned a couple of scripts that I wrote. The editors of a small
Linux magazine heard and found them interesting enough to ask me to
write an article about them. I accepted gladly, of course. I wrote
the article and submitted it and I was told to look for it on the
January issue. Sounds good, right?

The thing is I am starting to get a little nervous about it. You see,
programming is not my full time job. I dabble in it from time to
time, mostly to scratch my own itches, as they say. But, I know that
my code is probably far from being of professional quality. So, I was
wondering if some of you would be interested in taking a peak and
offer some suggestions for improving the quality and safety of the
code. Let me try to explain what they do.

Lets say, for example that you have, as I do, a large directory tree
that you want to compress containing data that you hardly ever use,
but that you want to have easy access to from time to time. In my
case, that directory tree contains the RAW image files that come from
my DSLR camera. Each of those files is about 10 MB. The total size
of that directory tree is about 45 GB, and it is constantly growing.
(Note: I store my finished, "processed", images on a different
directory tree. They are stored as JPEG files, so they are already
compressed.) How would you go about using compression to retake some
disk space on a situation like this one?

Well, one way I came up with was to write my own tool to do this job.
I created a program called 7sqz (7Squeeze) that can take care of this
task with ease. It is a Python script that navigates through a
directory tree compressing its contents only, not the actual
directories. As it enters each directory on the tree it saves all the
files on that directory on an archive on that same directory giving it
the name of the directory itself. If it finds that the directory
already has an archive file with the correct name it leaves it alone
and goes to the next directory, unless it also finds an uncompressed
file in it. When that happens it simply moves it into the existing
archive file, updating it inside the archive if it was already there.

I also created 7usqz which is the opposite counterpart of 7sqz. It
will simply go through a specified directory tree looking for archive
files named as the holding directory and will uncompress them,
essentially leaving the directory as it was before being squeezed.
Both 7sqz and 7usqz use p7zip for the actual compression, so you need
to have p7zip already installed.

You can obtain 7sqz from here:
http://rmcorrespond.googlepages.com/7sqz

And you can get 7usqz from here:
http://rmcorrespond.googlepages.com/7usqz

After downloading them, save them in a place like /usr/bin and make
sure they are executable.

To use 7sqz you could just give it a target directory as a parameter,
like this:

7sqz /home/some_directory

By default it will use the 7z format (which gives better compression
than zip), but you can use the zip format if you prefer by using the -
m option like this:

7sqz -m zip /home/some_directory

By default it will use Normal as the level of compression, but you can
use EXTRA or MAX if you prefer by using the -l option like this:

7sqz -l Extra /home/some_directory

By default it will just skip any file if it found an error during
compression and will log the error, but you can tell it to "Halt on
Error" with the -e option like this:

7sqz -e /home/some_directory

And of course, you can combine options as you please like this:

7sqz -m zip -l Max -e /home/some_directory

As I said, 7usqz is the opposite counterpart of 7sqz. To use it you
could just give it a target directory as a parameter, like this:

7usqz /home/some_directory

By default it will just skip any file if it found an error during
decompression and will log the error, but you can tell it to "Halt on
Error" with the -e option like this:

7usqz -e /home/some_directory

Please do a few, or better yet a lot of tests, before using it on a
directory that you cannot afford to loose. I believe it has all the
necessary safety precautions to protect your data, but I can''t
guaranty it. That is why I''m asking for your help. All I can say is
that I have never lost any data with it and that it works great for
me. What do you think?

解决方案

rm <rm**********@gmail.comwrote:

>
The thing is I am starting to get a little nervous about it. You see,
programming is not my full time job. I dabble in it from time to
time, mostly to scratch my own itches, as they say. But, I know that
my code is probably far from being of professional quality. So, I was
wondering if some of you would be interested in taking a peak and
offer some suggestions for improving the quality and safety of the
code. Let me try to explain what they do.

There are several places where you do something like this:
strng = "abcde"
print str(strng)

Why the calls to str()? These are already strings, and even if they
weren''t, "print" will convert its arguments.

You also have a couple of instances of:
print("Error Squeezing %s...")

The parentheses serve no purpose here, and are unidiomatic.

You have:
if ErrorHalt == True:
In almost every case, it''s better to write this as:
if ErrorHalt:

The same applies to:
if debug == 1:
This is really a boolean, and most would write that as:
if debug:

"Tuple" is spelled with one "p", but that''s being awfully picky...

Note that this:
def Save(self):
LogFile = open(self.logpath, "w")
for line in self.LogStrings:
LogFile.write(line)
LogFile.close()
can be written as:
def Save(self):
open(self.logpath, "w").writelines( self.LogStrings )
but that''s really micro-optimization.

Most people would argue that every line in a log file should be written and
flushed to disk immediately, and not saved until some later time. If your
program terminates unexpectedly, all of your log information will be lost.

You assume that p7zip will be installed in /usr/bin/7za. That''s not good.
On many of my systems, I install all new packages into /usr/local/bin. Some
people use /opt. You should really check the PATH to look for 7za.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.


Tim Roberts a écrit :

rm <rm**********@gmail.comwrote:

(snip)

You also have a couple of instances of:
print("Error Squeezing %s...")

The parentheses serve no purpose here, and are unidiomatic.

And FWIW, error messages should go to sys.stderr, not to sys.stdout
which is for normal program ouputs.

=>
# assert you imported sys before, of course
print >sys.stderr, "Error yadda yadda %s " % anything


On Nov 30, 2007 1:19 AM, Tim Roberts <ti**@probo.comwrote:

You also have a couple of instances of:
print("Error Squeezing %s...")

The parentheses serve no purpose here, and are unidiomatic.

I thought that with the eventual dropping of ''print'' as a statement in
Python 3, that writing it this way (as if it were a print function) is
preferred, since that will be one fewer thing to convert.

--

# p.d.


这篇关于哦不,我的代码正在发布......帮助!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆